Spring's Propertyplaceholderconfigurer Application

Source: Internet
Author: User

1. Propertyplaceholderconfigurer is the implementation of a bean factory post processor, which is an implementation of the Beanfactorypostprocessor interface. Propertyplaceholderconfigurer can place attribute values in a context (configuration file) in another separate standard Java properties file. Replaces the value in the specified properties file with ${key} in the XML file. In this case, only the properties file needs to be modified, not the XML configuration file.

2. In spring, you can use Propertyplaceholderconfigurer to include an external property file in an XML configuration file, or you can specify the encoding of an external file, such as:

<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"    > <property name= "Location" > <value>conf/sqlmap/jdbc.properties</value> </property> <property name= "fileencoding" > <value>UTF-8</value> </property></bean>

Of course, you can also introduce multiple properties files, such as:

<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Locations" > <list> <value>/WEB-INF/mail.properties</value> <val ue>classpath:conf/sqlmap/jdbc.properties</value>//Note the notation for these two value values </list> </property></ Bean>

3. For example, the contents of Jdbc.properties are:

jdbc.driverclassname=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost/mysqldb?useunicode=true&amp; characterencoding=utf-8&amp;zerodatetimebehavior=round;jdbc.username=rootjdbc.password=123456

4. In the spring configuration file, we can write this:

<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Locations" > <list> <value>classpath:conf/sqlmap/jdbc.properties </VALUE&G   T </list> </property></bean><bean id= "DataSource" class= " Org.apache.commons.dbcp.BasicDataSource "destroy-method=" Close "> <property name=" driverclassname "value=" ${ Jdbc.driverclassname} "/> <property name=" url "value=" ${jdbc.url} "/> <property name=" username "value=" ${ Jdbc.username} "/> <property name=" password "value=" ${jdbc.password} "/></bean>

5. In this way, a simple data source is set up. As you can see, the role of Propertyplaceholderconfigurer is to place the database configuration information pointed to by the placeholder in the tool defined in the bean.

6. Looking at the source code, you can see that the locations attribute is defined in Propertyplaceholderconfigurer's grandfather class Propertiesloadersupport, and location is only the setter method. Similar to this configuration, it is common in spring source programs.

Propertyplaceholderconfigurer if the property you want to use is not found in the specified properties file, it is also found in the Java System Class properties.

We can pass parameters to the spring configuration file via System.setproperty (key, value) or Java via-dnamevalue.


Spring's Propertyplaceholderconfigurer Application

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.