Propertyplaceholderconfigurer is an implementation of a bean factory post processor that can place some of the attribute values in the beanfactory definition into another separate standard Java properties file. This allows users to deploy applications with only the modification of key attributes (such as database URLs, user names, and passwords) in a property file without complex and dangerous modifications to the files used by the primary XML definition file or container.
Recently encountered a problem in the project, the program has more than one data source, each time from the tester to switch to the test machine to the spring configuration file to modify the data source link, is a very troublesome thing. The knowledge point was found when looking at the spring document. First, let's look at the example of Propertyplaceholderconfigurer.
Suppose that this section is configured in spring
<bean id= "DataSource1" parent= "Datasourcec3p0base" destroy-method= "Close" lazy-init= "true" > <property name= "Driverclass" value= "Oracle.jdbc.driver.OracleDriver"/> <property name= "Jdbcurl" value= Localhost:1521:orcl "/> <property name=" user "value=" testUser "/> <property name=" password "value=" testPwd "/> </bean>
First step:
Create a springconfigtest.properties file with the following contents
Datasource1.driverclass=oracle.jdbc.driver.oracledriver Datasource1.jdbcurl=jdbc:oracle:thin: @localhost: 1521:o RCL Datasource1.user=testuser Datasource1.password=testpwd
Step Two:
To add a propertyplaceholderconfigurer configuration to the spring configuration file
<bean id= "Preferencesconfig" class= "Org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer "> <property name=" Location "> <value>springConfigTest.properties</value> </property> </bean>
Step Three:
Modify DataSource1
<bean id= "DataSource1" parent= "Datasourcec3p0base" destroy-method= "Close" lazy-init= "true" > <property name= "Driverclass" value= "${datasource1.driverclass}"/> <property name= "Jdbcurl" value= "${DATASOURCE1.JDBCURL}"/ > <property name= "User" value= "${datasource1.user}"/> <property name= "password" value= "${" Datasource1.password} "/> </bean>
Configuration complete.
When you have multiple data sources, you can create two properties files, one point to the test machine data source, one point to the pilot's data source, and then just switch the configuration file OK.
Of course, you can also do some other configuration.