This is very common in the configuration file in spring.
Context:property-placeholder greatly facilitates the configuration of our database.
Just add a phrase to the spring's configuration file: <context:property-placeholder?location= "Classpath:jdbc.properties"/> Here the location value is the location of the parameter profile, and the parameter profile is usually placed in the SRC directory, and the format of the parameter profile is the same as the Java generic parameter profile, which is the form of a key-value pair, for example: #jdbc配置 test.jdbc.driverclassname= Com.mysql.jdbc.Driver test.jdbc.url=jdbc:mysql://localhost:3306/test Test.jdbc.username=root test.jdbc.password= Root
This allows you to set a value for the properties of a spring-configured bean, for example, Spring has a JDBC data source class Drivermanagerdatasource
This defines the bean in the configuration file:
<bean id= "Testdatasource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname" value= "${test.jdbc.driverclassname}"/>
<property name= "url" value= "${test.jdbc.url}"/>
<property name= "username" value= "${test.jdbc.username}"/>
<property name= "Password" value= "${test.jdbc.password}"/>
</bean> This is also convenient to modify, but also uniform this specification.
Also note that if you encounter the following problem:
Both A and B modules have their own spring XML configuration, and each has its own configuration file:
The spring configuration file for module A is as follows:
<?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 >
Conf/conf_a.properties:
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
The spring configuration file for Module B is as follows:
<?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 >
Conf/conf_b.properties:
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
It is normal to run the A module alone, or to run the B module separately, but after integrating A and B two modules, the spring container will not start:
Could not resolve placeholder ' moduleb.jdbc.driverClassName ' in string value ' ${moduleb.jdbc.driverclassname} '
Cause: Spring containers Use the discovery mechanism of reflection scans, A bean that has a org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in the spring container is detected to stop the remaining propertyplaceholderconf Scanning of Igurer
A maximum of one context:property-placeholder can be defined in the spring container, or there will be that error, how to solve the problem above.
A and B modules removed
<context:property-placeholder location= "Classpath*:conf/conf_b.properties"/>
<?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>