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
另外需要注意的是,如果遇到下面着着这种问题:
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块的Spring配置文件如下:
- <?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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <context:property-placeholder location="classpath*:conf/conf_a.properties"/>
- <bean class="com.xxx.aaa.Bean1"
- p:driverClassName="${modulea.jdbc.driverClassName}"
- p:url="${modulea.jdbc.url}"
- p:username="${modulea.jdbc.username}"
- p:password="${modulea.jdbc.password}"/>
- </beans>
- modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
- modulea.jdbc.username=cartan
- modulea.jdbc.password=superman
- modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
B模块的Spring配置文件如下:
- <?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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
- <bean class="com.xxx.bbb.Bean1"
- p:driverClassName="${moduleb.jdbc.driverClassName}"
- p:url="${moduleb.jdbc.url}"
- p:username="${moduleb.jdbc.username}"
- p:password="${moduleb.jdbc.password}"/>
- </beans>
- moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
- moduleb.jdbc.username=cartan
- moduleb.jdbc.password=superman
- moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
spring容器中最多单独运行A模块,或单独运行B模块都是正常的,但将A和B两个模块集成后运行,Spring容器就启动不了了:Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"
原因:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描只能定义一个context:property-placeholder,不然就出现那种个错误,那如何来解决上面的问题呢?
A和B模块去掉
- <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
然后重新写个xml:
- <?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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <context:property-placeholder location="classpath*:conf/conf*.properties"/>
- <import resource="a.xml"/>
- <import resource="b.xml"/>
- </beans>