Spring preferencesplaceholderconfigurer Parameter Placeholder configuration

Source: Internet
Author: User

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.

Related Article

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.