spring中<context:property-placeholder>的作用

context:property-placeholder方便了我们数据库的配置。

spring 3.0 后提供了 注解配置引入,<context:property-placeholder/>,方便。但是往往第一次使用时会遇到此属性配置进applicationContext.xml文件中,会出现红叉:
“The prefix "context" for element "context:property-placeholder" is not bound. ”

只需要在文件头中引入:xmlns:context="http://www.springframework.org/schema/context" 即可。
举例如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <context:property-placeholder location="classpath:jdbc.properties"/>
   <bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
   </bean>
</beans>

jdbc.properties:
#jdbc配置
jdbc.driverClassName = Oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@//localhost:1521/orcl
jdbc.username = scotter
jdbc.password = 123

posted @ 2017-01-09 17:34  爱吃空心菜  阅读(734)  评论(0编辑  收藏  举报