org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。由這個類別,您能夠將一些組態設定,移出至.properties檔案中,如此的安排可讓XML定義檔負責系統相關設定,而.properties檔可 以做爲客戶根據需求,自定義一些相關的參數。實際上,PropertyPlaceholderConfigurer起的做用就是將佔位符指向的數據庫配置 信息放在bean中定義的工具。java
(1)使用如下方式讀取單個本地文件到xml中:spring
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>file:/opt/config/config1.properties</value> </property> </bean>
(2)使用如下方式讀取多個本地文件到xml中:數據庫
<!-- 使用spring提供的PropertyPlaceholderConfigurer讀取properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 屬性名是 locations,使用子標籤<list></list>能夠指定多個數據庫的配置文件,這裏指定了一個-->
<property name="locations"> <list> <value>file:/opt/config/config1.properties</value>
<value>file:/opt/config/config2.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean> <dubbo:application name="lease-risk-consumer"/> <!-- 讀取本地配置文件 --> <dubbo:registry address="${dubbo.registry.address}"/>
注意讀取單個配置文件,使用location,多個使用locationsapp
上面是介紹org.springframework.beans.factory.config.PropertyPlaceholderConfigurer工具類的具體使用,下面淺析Spring框架下PropertyPlaceholderConfigurer類框架
package org.springframework.beans.factory.config; import java.util.Properties; import java.util.Set; import org.springframework.beans.BeansException; import org.springframework.core.Constants; import org.springframework.util.PropertyPlaceholderHelper; import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; import org.springframework.util.StringValueResolver; public class PropertyPlaceholderConfigurer extends PlaceholderConfigurerSupport { /** 是否覆蓋proprties文件 */ public static final int SYSTEM_PROPERTIES_MODE_NEVER = 0; /** * 默認值,不存在時覆蓋 */ public static final int SYSTEM_PROPERTIES_MODE_FALLBACK = 1; /**覆蓋proprties文件*/ public static final int SYSTEM_PROPERTIES_MODE_OVERRIDE = 2; private static final Constants constants = new Constants(PropertyPlaceholderConfigurer.class); private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK; private boolean searchSystemEnvironment = true; public void setSystemPropertiesModeName(String constantName) throws IllegalArgumentException { this.systemPropertiesMode = constants.asNumber(constantName).intValue(); } public void setSystemPropertiesMode(int systemPropertiesMode) { this.systemPropertiesMode = systemPropertiesMode; } public void setSearchSystemEnvironment(boolean searchSystemEnvironment) { this.searchSystemEnvironment = searchSystemEnvironment; } protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) { 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); } return propVal; } protected String resolvePlaceholder(String placeholder, Properties props) { return props.getProperty(placeholder); } protected String resolveSystemProperty(String key) { try { String value = System.getProperty(key); if (value == null && this.searchSystemEnvironment) { value = System.getenv(key); } return value; } catch (Throwable ex) { if (logger.isDebugEnabled()) { logger.debug("Could not access system property '" + key + "': " + ex); } return null; } } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props); this.doProcessProperties(beanFactoryToProcess, valueResolver); } @Deprecated protected String parseStringValue(String strVal, Properties props, Set<?> visitedPlaceholders) { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper( placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver(props); return helper.replacePlaceholders(strVal, resolver); } private class PlaceholderResolvingStringValueResolver implements StringValueResolver { private final PropertyPlaceholderHelper helper; private final PlaceholderResolver resolver; public PlaceholderResolvingStringValueResolver(Properties props) { this.helper = new PropertyPlaceholderHelper( placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); this.resolver = new PropertyPlaceholderConfigurerResolver(props); } public String resolveStringValue(String strVal) throws BeansException { String value = this.helper.replacePlaceholders(strVal, this.resolver); return (value.equals(nullValue) ? null : value); } } private class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver { private final Properties props; private PropertyPlaceholderConfigurerResolver(Properties props) { this.props = props; } public String resolvePlaceholder(String placeholderName) { return PropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName, props, systemPropertiesMode); } } }
首先看下PropertyPlaceholderConfigurer類的結構ide
一、注入會初始化property的location屬性工具
public void setLocation(Resource location) { this.locations = new Resource[] {location}; } public void setLocations(Resource[] locations) { this.locations = locations; }
二、SystemPropertiesModeName:SystemPropertiesModeName屬性用來指定系統變量(System.getProperty)是否覆蓋proprties文件。默認是在配置文件裏找不到時使用。三個選項分別是:this
SYSTEM_PROPERTIES_MODE_NEVER(不覆蓋)spa
SYSTEM_PROPERTIES_MODE_FALLBACK(默認值,不存在時覆蓋)debug
SYSTEM_PROPERTIES_MODE_OVERRIDE(覆蓋)
從源碼中能夠看出searchSystemEnvironment屬性的默認值爲true,即容許環境變量覆蓋properties中的值。