This is very common in the spring configuration file.
Context:property-placeholder greatly facilitates the configuration of our database.
[HTML]View PlainCopy
- Just add a line to the spring configuration file:<context:property-placeholder? location="classpath:jdbc.properties"/> The location value is the position of the parameter profile, and the parameter profile is usually placed in the SRC directory. The format of the parameter configuration file is the same as the Java common parameter configuration file, which is the form of key-value pairs, 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 values for the properties of the bean that is configured for spring, such as 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 modification is also convenient, but also unified this specification.
It is also important to note that if you encounter the following problem:
Both A and B modules have their own spring XML configuration, each with its own configuration file:
The spring configuration file for the A module is as follows:
[HTML]View PlainCopy
- <? 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:
[HTML]View PlainCopy
- 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 the B module is as follows:
[HTML]View PlainCopy
- <? 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:
[HTML]View PlainCopy
- 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 a module alone or 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: The spring container uses the discovery mechanism of reflection scanning, A bean that detects a org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in the spring container stops the remaining propertyplaceholderconf Scanning of Igurer
Only one context:property-placeholder can be defined in the spring container, or there will be such a mistake, how to solve the above problem?
A and B modules removed
[HTML]View PlainCopy
- <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
Then write an XML again:
[HTML]View PlainCopy
- <? 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>
Context:property-placeholder