On the propertyplaceholderconfigurer of spring

Source: Internet
Author: User

In large-scale projects, we tend to manage the configuration information of our system in a general way by configuring configuration information with a cfg.properties file, and then automatically reading the key in the Cfg.properties configuration file when our system is initialized Value (key-value pairs), and then customize the initialization of our system.

?? So in general, we use Java.util.Properties, which is Java's own. Often a problem is that every time we load, we need to manually read this configuration file, one coding trouble, and the code is not elegant, often we will also create a class to read, and store these configuration information.

? But, in my opinion, these are the things that spring has, and you have to reinvent the wheel and pay the bills. And not necessarily less convenient to maintain and manage upgrades.
Use of the common comparison is poor, each enterprise has a variety of enterprise development norms, so that a child, learning costs are also improved. (This is nonsense).

Spring provides a propertyplaceholderconfigurer

?? This class is a subclass of Beanfactorypostprocessor. (Do not understand their own Niang, can understand not understand, below I will say about)

Its main principle is in the. When the spring container is initialized, it reads either XML or annotation to initialize the bean.
When initialized, this propertyplaceholderconfigurer will intercept the initialization of the bean,
The configuration ${pname} is replaced when initialized, and is replaced by the configuration in our properties. This enables the substitution of expressions.

After understanding the principles, let's look at some of the ways it's done.

Create a new cfg.properties under our classpath.

Contents of the #cfg. Properties configuration file Username=jay    password=123

Here is the spring configuration file code

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >    <Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <!--scenarios taken for reading a configuration file  -        < Propertyname= "Location"value= "Classpath:cfg.properties"></ Property>        <!--processing scheme for reading more than two configuration files  -        <!--< property name= "Locations" > <list> <V Alue>classpath:cfg.properties</value> <value>classpath:cfg2.properties</val ue> </list> </property > -    </Bean></Beans>
//Test Code@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationCtx.xml") Public classspringctxtst {@Value ("${username}")    PrivateString uname; @Value ("${password}")    PrivateString pwd; @Test Public voidTest () {System.out.println ("Username:" +uname); System.out.println ("Password:" +pwd); }}

Console output

username:jay Password:123

Of course, spring also provides us with another solution, and we can write the following code directly in the spring configuration file.

<beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd"><!--simplifies the configuration file in this way--<context:property-placeholder location= "classpath:cfg.properties,classpath:cfg2.properties"/ ></beans>
Important note

?? We know that, whether using propertyplaceholderconfigurer or context:property-placeholder this way, you need to remember that The spring framework not only reads the key-value pairs in our configuration file, but also reads the system initialization information from the JVM. Sometimes, we need to set the configuration key to a set of naming rules, such as

Project name. Group name. Function name = configuration value org.team.tfunction=0001

This way to minimize conflicts with system configuration information!
?? At the same time, we can also use the following configuration method to configure, here I am with never meaning is not to read the system configuration information. If

<location= "Classpath:cfg.properties,classpath:cfg2.properties"
System-properties-mode= "Never"/>

On the propertyplaceholderconfigurer of spring

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.