Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. By this category, you can move some of the configuration settings out to the. properties file, so that the XML definition file is responsible for system-related settings, and the. Properties document can be used as a customer to customize some relevant parameters as required. In fact, the role of Propertyplaceholderconfigurer is to place the database configuration information pointed to by the placeholder in the tool defined in the bean.
(1) Use the following method to read a single local file into the XML:
<BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Location"> <value>File:/opt/config/config1.properties</value> </ Property> </Bean>
(2) Read multiple local files into XML using the following method:
<!--using the spring-providedPropertyplaceholderconfigurerReadProperties-
<Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><!--property name isLocations, using sub-tags <list></list>You can specify a configuration file for multiple databases, where you specify a-
< Propertyname= "locations"> <List> <value>File:/opt/config/config1.properties</value>
<value>file:/opt/config/config2.properties</value> </List> </ Property> < Propertyname= "Ignoreunresolvableplaceholders"value= "true"/> </Bean> <dubbo:applicationname= "Lease-risk-consumer"/><!--read the local configuration file--<Dubbo:registryAddress= "${dubbo.registry.address}"/>
Note reading a single profile, using location, multiple use locations
Above is the introduction of the Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer tool class specific use, the following analysis of the spring framework Propertyplaceholderconfi Gurer class
PackageOrg.springframework.beans.factory.config;Importjava.util.Properties;ImportJava.util.Set;Importorg.springframework.beans.BeansException;Importorg.springframework.core.Constants;ImportOrg.springframework.util.PropertyPlaceholderHelper;ImportOrg.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;ImportOrg.springframework.util.StringValueResolver; Public classPropertyplaceholderconfigurerextendsPlaceholderconfigurersupport {/**whether to overwrite the proprties file*/ Public Static Final intSystem_properties_mode_never = 0; /*** Default value, overwrite when not present*/ Public Static Final intSystem_properties_mode_fallback = 1; /**Overwrite proprties file*/ Public Static Final intSystem_properties_mode_override = 2; Private Static FinalConstants Constants =NewConstants (Propertyplaceholderconfigurer.class); Private intSystempropertiesmode =System_properties_mode_fallback; Private BooleanSearchsystemenvironment =true; Public voidSetsystempropertiesmodename (String constantname)throwsIllegalArgumentException { This. Systempropertiesmode =Constants.asnumber (constantname). Intvalue (); } Public voidSetsystempropertiesmode (intSystempropertiesmode) { This. Systempropertiesmode =Systempropertiesmode; } Public voidSetsearchsystemenvironment (Booleansearchsystemenvironment) { This. searchsystemenvironment =searchsystemenvironment; } protectedString Resolveplaceholder (String placeholder, Properties props,intSystempropertiesmode) {String PropVal=NULL; if(Systempropertiesmode = =system_properties_mode_override) {PropVal=resolvesystemproperty (placeholder); } if(PropVal = =NULL) {PropVal=resolveplaceholder (placeholder, props); } if(PropVal = =NULL&& Systempropertiesmode = =system_properties_mode_fallback) {PropVal=resolvesystemproperty (placeholder); } returnPropVal; } protectedstring Resolveplaceholder (string placeholder, Properties props) {returnprops.getproperty (placeholder); } protectedstring Resolvesystemproperty (String key) {Try{String value=System.getproperty (key); if(Value = =NULL&& This. Searchsystemenvironment) {Value=system.getenv (key); } returnvalue; } Catch(Throwable ex) {if(logger.isdebugenabled ()) {Logger.debug ("Could Not access system Property" + Key + "':" +ex); } return NULL; } }@Overrideprotected voidprocessproperties (configurablelistablebeanfactory beanfactorytoprocess, Properties props)throwsbeansexception {stringvalueresolver valueresolver=Newplaceholderresolvingstringvalueresolver (props); This. Doprocessproperties (beanfactorytoprocess, valueresolver); } @DeprecatedprotectedString Parsestringvalue (String strval, Properties props, set<?>visitedplaceholders) {Propertyplaceholderhelper Helper=NewPropertyplaceholderhelper (Placeholderprefix, Placeholdersuffix, Valueseparator, Ignoreunresolvablepla Ceholders); Placeholderresolver Resolver=Newpropertyplaceholderconfigurerresolver (props); returnhelper.replaceplaceholders (strval, resolver); } Private classPlaceholderresolvingstringvalueresolverImplementsStringvalueresolver {Private FinalPropertyplaceholderhelper Helper; Private FinalPlaceholderresolver Resolver; PublicPlaceholderresolvingstringvalueresolver (Properties props) { This. Helper =NewPropertyplaceholderhelper (Placeholderprefix, Placeholdersuffix, Valueseparator, IGNOREUNRESOLVAB Leplaceholders); This. Resolver =Newpropertyplaceholderconfigurerresolver (props); } PublicString Resolvestringvalue (String strval)throwsbeansexception {String value= This. Helper.replaceplaceholders (Strval, This. Resolver); return(Value.equals (nullvalue)?NULL: Value); } } Private classPropertyplaceholderconfigurerresolverImplementsPlaceholderresolver {Private FinalProperties props; PrivatePropertyplaceholderconfigurerresolver (Properties props) { This. props =props; } Publicstring Resolveplaceholder (String placeholdername) {returnPropertyplaceholderconfigurer. This. Resolveplaceholder (placeholdername, props, Systempropertiesmode); } }}
First look at the structure of the Propertyplaceholderconfigurer class
1. Injection initializes the property's location attribute
Public void setlocation (Resource location) { thisnew resource[] {location};} Public void setlocations (resource[] locations) { this. locations = locations;}
2. The Systempropertiesmodename:systempropertiesmodename property is used to specify whether the system variable (system.getproperty) overwrites the proprties file. The default is to use when it is not found in the configuration file. The three options are:
System_properties_mode_never (not covered)
System_properties_mode_fallback (default, overwrite if not present)
System_properties_mode_override (overlay)
As you can see from the source code, the default value of the Searchsystemenvironment property is true, which is to allow the environment variable to overwrite the value in properties.
SPRINGMVC load XML file read local properties configuration file