Use of the Spring property placeholder Propertyplaceholderconfigurer
1, a simple demo
1.1, create Conf.xml <?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<!--
Use the Location property to define a single configuration file
<property name= "Location" >
<value>classpath:/com/zsw/config/jdbc.properties</value>
</property>
-
<!--define multiple profiles with the locations property--
<property name= "Locations" >
<list>
<value>classpath:/com/zsw/config/jdbc.properties</value>
</list>
</property>
</bean>
<bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "url" >
<value>${database.url}</value>
</property>
<property name= "Driverclassname" >
<value>${database.driver}</value>
</property>
<property name= "username" >
<value>${database.user}</value>
</property>
<property name= "Password" >
<value>${database.password}</value>
</property>
</bean>
</beans>
1.2. Create a jdbc.properties file Database.driver=com.mysql.jdbc.driver
Database.url=jdbc:mysql://localhost:3306/right?useunicode=true&autoreconnect=true&characterencoding= UTF-8
Database.user=root
Database.password=root
jdbc.pool.c3p0.acquire_increment=2
Jdbc.pool.c3p0.max_size=20
jdbc.pool.c3p0.min_size=2
jdbc.pool.c3p0.preferred_test_query= ' SELECT 1 '
jdbc.pool.c3p0.idle_connection_test_period=18000
jdbc.pool.c3p0.max_idle_time=25000
1.3. Create Config.java package com.zsw.config;
Import Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
Import Org.springframework.beans.factory.xml.XmlBeanFactory;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.core.io.FileSystemResource;
Import Org.springframework.jdbc.datasource.DriverManagerDataSource;
public class Config {
public static void Main (string[] args) {
Xmlbeanfactory factory = new Xmlbeanfactory (New Filesystemresource ("Src/com/zsw/config/conf.xml"));
If you want to use it in beanfactory, the Bean factory post-processor must be run manually:
Propertyplaceholderconfigurer cfg = new Propertyplaceholderconfigurer ();
Cfg.setlocation (New Filesystemresource ("Src/com/zsw/config/jdbc.properties"));
Cfg.postprocessbeanfactory (Factory);
Drivermanagerdatasource DataSource = (drivermanagerdatasource) factory.getbean ("DataSource");
System.out.println (Datasource.getdriverclassname ());
System.out.println (Datasource.getusername ());
Note that ApplicationContext is able to automatically identify and apply beanfactorypostprocessor-implemented beans deployed on it. This means that it is very convenient to apply propertyplaceholderconfigurer when using ApplicationContext. For this reason, it is recommended that you want to use this or another bean
Factory Postprocessor users use ApplicationContext instead of Beanfactroy.
ApplicationContext context = new Classpathxmlapplicationcontext ("Com/zsw/config/conf.xml");
Drivermanagerdatasource DataSource2 = (drivermanagerdatasource) context.getbean ("DataSource");
System.out.println (Datasource2.getusername ());
}
}
Propertyplaceholderconfigurer multiple configuration modes in 2.Spring
2.1 Configuring a single properties file < bean id= "propertyconfigurerforanalysis" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
< name= "Location" >
< value>classpath:/spring/include/dbquery.properties</value>
</property>
</bean> where classpath is a reference to the file notation under the SRC directory. 2.2 When there are multiple properties files, the configuration requires the use of locations:
< bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > < property name= "Locations" > < list> < value>classpath:/spring/include/jdbc- parms.properties</value> < value>classpath:/spring/include/base-config.properties</value> </list> </property> </bean>
2.3, next we want to use multiple propertyplaceholderconfigurer to disperse the configuration, to achieve the integration of multiple projects under the decentralized properties file, the configuration is as follows: <bean id= " PropertyConfigurerForProject1 " class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
< Property name= "Order" value= "1" />
<property name= " Ignoreunresolvableplaceholders " value=" true " />
<property name = "Location" >
<value>classpath:/spring/include/ Dbquery.properties</value>
</property>
</bean>
< bean id= "PropertyConfigurerForProject2" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
< property name= "order" value= "2"/>
< property name= "Ignoreunresolvableplaceholders" value= "true"/>
< property name= "Locations" >
< list>
< value>classpath:/spring/include/jdbc-parms.properties</value>
< value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
Where the Order property represents its load order, and ignoreunresolvableplaceholders to ignore unresolved Placeholder, such as configuring multiple Propertyplaceholderconfigurer, You need to set to True