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
PackageCom.zsw.config;
ImportOrg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
ImportOrg.springframework.beans.factory.xml.XmlBeanFactory;
ImportOrg.springframework.context.ApplicationContext;
ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;
ImportOrg.springframework.core.io.FileSystemResource;
ImportOrg.springframework.jdbc.datasource.DriverManagerDataSource;
PublicclassConfig {
PublicStaticvoidMainString[] args) {
Xmlbeanfactory factory =NewXmlbeanfactory (NewFilesystemresource ("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 =NewPropertyplaceholderconfigurer ();
Cfg.setlocation (NewFilesystemresource ("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 can 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 =NewClasspathxmlapplicationcontext ("Com/zsw/config/conf.xml");
Drivermanagerdatasource DataSource2 = (drivermanagerdatasource) Context.getbean ("DataSource");
System.out.println (Datasource2.getusername ());
}
}
2.propertyplaceholderconfigurer Multiple configuration methods in 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.pr Operties</value> <value>classpath:/spring/include/base-config.properties</value> </li St> </property></bean>
2.3, Next we will use multiple propertyplaceholderconfigurer to disperse the configuration, to achieve the integration of multiple projects under the decentralized properties file, which is configured as follows:
<bean id= class = " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ";
<property name= "order" value= "1" />
<property name=< Span style= "Color:rgb (255,0,255)" > "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
Use of the Spring property placeholder Propertyplaceholderconfigurer