Spring Propertyplaceholderconfigurer Usage-replacing variables in the spring configuration file with system variables

Source: Internet
Author: User

The previous article said about the use of propertyplaceholderconfigurer classes in spring http://blog.csdn.net/kongxx/archive/2010/08/26/5842009.aspx

In some cases, however, our properties are not configured in the properties file, but are set in the Java system environment through the Java startup-dname=value parameter, where in Java we can use System.getproperty ( Name) to get the property value, and in spring we can get it through the Propertyplaceholderconfigurer class.

1. First create a Java Bean

Package test; Import Org.apache.commons.lang.builder.ToStringBuilder; public class Mybean {private string name; private string Prop1, private string prop2; private string prop3; public string GetName () {return name.} public void SetName (string name) {this.name = name.} public String GetProp1 () {return PROP1 ; SETPROP1 (String prop1) {THIS.PROP1 = Prop1} public string GetProp2 () {return prop2;} public void Setpro P2 (string prop2) {this.prop2 = PROP2;} public String getProp3 () {return prop3;} public void SetProp3 (String prop3) {T HIS.PROP3 = PROP3; @Override public String toString () {return tostringbuilder.reflectiontostring (this);}}

2. Create spring.xml files

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.0.xsd "default-lazy-init=" true "> <bean id=" Propertyconfigurer "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> < Property Name= "Locations" > <value>classpath:test/spring.properties</value> </property> < Property name= "Systempropertiesmode" > <value>1</value> </property> <property name= " Searchsystemenvironment "> <value>true</value> </property> <property name=" Ignoreunresolvableplaceholders "> <value>true</value> </property> </bean> <bean id=" Mybean "class=" test. Mybean "> <property name=" name "><value>${name}</value></property> <property name="Prop1" ><value>${prop1}</value></property> <property name= "Prop2" ><value>${ prop2}</value></property> <property name= "PROP3" ><value>${prop3}</value></ Property> </bean> </beans> 

Use ${NAME},${PROPX} in the configuration file to indicate that you need to replace the content in the properties file

3. Create spring.properties file where variables can recursively refer to other variables defined in the current properties file

Name=kongxx prop1=111 prop2=${prop1}222 prop3=${prop2}333

4. Write a test procedure

Package test; import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {public static void main (string[] args) {system.setproperty ("name", "Mandy"); System.setproperty ("Prop1", "111"); System.setproperty ("Prop2", "222"); System.setproperty ("Prop3", "333"); ApplicationContext CTX = new Classpathxmlapplicationcontext ("/test/spring.xml"); Mybean Mybean = (Mybean) ctx.getbean ("Mybean"); System.out.println (Mybean); } }

I'm going to go here. System.setproperty (key) is used to simulate the passing of parameters through-D in Java before booting. Run the test program, the output is as follows:

Test. MYBEAN@1649B44[NAME=KONGXX,PROP1=111,PROP2=222,PROP3=333]

Here actually spring is the value in the system environment that is used in the configuration of the properties file that is ignored.

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.