Previous ArticleArticleAbout the use http://blog.csdn.net/kongxx/archive/2010/08/26/5842009.aspx of propertyplaceholderconfigurer class in spring
However, in some cases, our properties are not configured in the properties file, but are set in the Java System Environment through the-dname = value parameter at Java startup, in this case, we can use system. getproperty (name) to get the property value, and in spring we can get it through the propertyplaceholderconfigurer class.
1. Create a Java Bean
Package test; <br/> Import Org. apache. commons. lang. builder. tostringbuilder; <br/> public class mybean {<br/> private string name; <br/> private string prop1; <br/> private string prop2; <br/> private string prop3; <br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public String getprop1 () {<br/> return prop1; <br/>}< br/> Public void setprop1 (string prop1) {<br/> This. prop1 = prop1; <br/>}< br/> Public String getprop2 () {<br/> return prop2; <br/>}< br/> Public void setprop2 (string prop2) {<br/> This. prop2 = prop2; <br/>}< br/> Public String getprop3 () {<br/> return prop3; <br/>}< br/> Public void setprop3 (string prop3) {<br/> This. prop3 = prop3; <br/>}< br/> @ override <br/> Public String tostring () {<br/> return tostringbuilder. reflectiontostring (this); <br/>}< br/>}
2. Create a spring. xml file
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" <br/> default-Lazy-init = "true"> <br/> <bean id = "propertyconfigurer" class = "org. springframework. beans. factory. config. propertyplaceholderconfigurer "> <br/> <property name =" locations "> <br/> <value> classpath: Test/spring. properties </value> <br/> </property> <br/> <property name = "systempropertiesmode"> <br/> <value> 1 </value> <br/> </property> <br/> <property name = "searchsystemenvironment"> <br/> <value> true </value> <br/> </property> <br/> <property name = "ignoreunresolvableplaceholders"> <br/> <value> true </value> <br/> </property> <br/> </bean> <br/> <bean id = "mybean" class = "test. mybean "> <br/> <property name =" name "> <value >$ {name} </value> </property> <br/> <property name =" prop1 "> <value >$ {prop1} </value> </property> <br/> <property name =" prop2 "> <value >$ {prop2} </value> </property> <br/> <property name = "prop3"> <value >$ {prop3} </value> </property> <br/> </bean> <br/> </beans>
Use $ {name} and $ {propx} in the configuration file to replace the content in the properties file.
3. Create the spring. properties file. The variables here can reference other variables defined in the current properties file recursively.
Name = kongxx <br/> prop1 = 111 <br/> prop2 =$ {prop1} 222 <br/> prop3 =$ {prop2} 333
4. Write a testProgram
Package test; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> public class test {<br/> Public static void main (string [] ARGs) {<br/> system. setproperty ("name", "Mandy"); <br/> system. setproperty ("prop1", "111"); <br/> system. setproperty ("prop2", "222"); <br/> system. setproperty ("prop3", "333"); <br/> applicationcontext CTX = new classpathxmlapplicationcontext (<br/> "/test/spring. XML "); <br/> mybean = (mybean) CTX. getbean ("mybean"); <br/> system. out. println (mybean); <br/>}< br/>
Here I use system. setproperty (key) to simulate passing parameters through-D in Java before starting. Run the test program. The output is as follows:
Test. mybean @ 1649b44 [name = kongxx, prop1 = 111, prop2 = 222, prop3 = 333]
In fact, spring is the value in the system environment Used for the configuration in the properties file that is ignored.