Use of spring property placeholder propertyplaceholderconfigurer

Source: Internet
Author: User

 

Use of 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>
-->

<! -- Use the locations attribute to define multiple configuration files -->
<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 the 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 "));
// To be used in beanfactory, 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 can automatically identify and deploy beanfactorypostprocessor beans on the application. This means that it is very convenient to apply propertyplaceholderconfigurer when applicationcontext is used. For this reason, we recommend that you use this or another bean.
// The factory postprocessor user uses applicationcontext instead of beanfactroy.
Applicationcontext context = new classpathxmlapplicationcontext ("com/zsw/config/CONF. xml ");
Drivermanagerdatasource performance2 = (drivermanagerdatasource) Context. getbean ("datasource ");
System. Out. println (performance2.getusername ());
}
 
}

 

2. Various configuration methods of propertyplaceholderconfigurer in spring

2.1 configure a single properties File

<Bean id = "propertypolicerforanalysis" class = "org. springframework. Beans. Factory. config. propertyplaceholderconfigurer">
<Property name = "location">
<Value> classpath:/spring/include/dbquery. properties </value>
</Property>
</Bean> classpath references the file writing method under the src directory. 2.2 When multiple properties files exist, you need to use locations for Configuration:
<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 will use multiple propertyplaceholderconfigurer for decentralized configuration to integrate multiple scattered properties files under multiple projects. The configuration is as follows:

<Bean id = "propertypolicerforproject1" class = "org. springframework. Beans. Factory. config. propertyplaceholderpolicer">
<Property name = "order" value = "1"/>
<Property name = "ignoreunresolvableplaceholders" value = "true"/>
<Property name = "location">
<Value> classpath:/spring/include/dbquery. properties </value>
</Property>
</Bean>

 

<Bean id = "propertypolicerforproject2" class = "org. springframework. Beans. Factory. config. propertyplaceholderpolicer">
<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>

 

The Order attribute indicates the loading order, while the ignoreunresolvableplaceholders indicates whether to ignore unresolvable placeholder. If multiple propertyplaceholderregistrers are configured, the value must be set to true.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.