1. PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor接口的一個實現。PropertyPlaceholderConfigurer能夠將上下文(配置文 件)中的屬性值放在另外一個單獨的標準java Properties文件中去。在XML文件中用${key}替換指定的properties文件中的值。這樣的話,只須要對properties文件進 行修改,而不用對xml配置文件進行修改。javascript
2.在Spring中,使用PropertyPlaceholderConfigurer能夠在XML配置文件中加入外部屬性文件,固然也能夠指定外部文件的編碼,如:html
1 <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 2 <property name="location"> 3 <value>conf/jdbc.properties</value> 4 </property> 5 <property name="fileEncoding"> 6 <value>UTF-8</value> 7 </property> 8 </bean> 9 10
3.PropertyPlaceholderConfigurer起的做用就是將佔位符指向的數據庫配置信息放在bean中定義的工具。java
4.查看源代碼,能夠發現,locations屬性定義在PropertyPlaceholderConfigurer的祖父類 PropertiesLoaderSupport中,而location只有 setter方法。相似於這樣的配置,在spring的源程序中很常見的。spring
PropertyPlaceholderConfigurer若是在指定的Properties文件中找不到你想使用的屬性,它還會在Java的System類屬性中查找。數據庫
咱們能夠經過System.setProperty(key, value)或者java中經過-Dnamevalue來給Spring配置文件傳遞參數。api
實例:oracle
<bean class="com.slp.util.SpringContextHolder"/> <bean id="propertyConfigurer" class="com.slp.util.CustomizedPropertyConfigurer"> <property name="locations"> <list> <value>classpath:conf/jdbc.properties</value> <value>classpath:conf/config.properties</value> <value>classpath:conf/server.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8"/> </bean>
package com.slp.util; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map<String,String> ctxPropMap; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropMap = new HashMap<>(); for (Object key : props.keySet()){ String keyStr = key.toString(); String value = String.valueOf(props.get(keyStr)); ctxPropMap.put(keyStr,value); } } public static String getCtxProp(String name) { return ctxPropMap.get(name); } public static Map<String, String> getCtxPropMap() { return ctxPropMap; } }
//使用的時候以下便可 CustomizedPropertyConfigurer.getCtxProp("xxx")
org.springframework.beans.factory.configapp
java.lang.Object-->ide
org.springframework.core.io.support.PropertiesLoaderSupport-->工具
org.springframework.beans.factory.config.PropertyResourceConfigurer-->
org.springframework.beans.factory.config.PlaceholderConfigurerSupport-->
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
以上即是PropertyPlaceholderConfigurer類的繼承關係,
All Implemented Interfaces:所實現的接口
Aware, BeanFactoryAware, BeanNameAware, BeanFactoryPostProcessor, Ordered, PriorityOrdered
Direct Known Subclasses:(直接的已知的子類)
PreferencesPlaceholderConfigurer
直接的子類:PreferencesPlaceholderConfigurer
public class PropertyPlaceholderConfigurer
extends PlaceholderConfigurerSupport
PlaceholderConfigurerSupport subclass that resolves ${...} placeholders against local properties and/or system properties and environment variables.
PlaceholderConfigurerSupport 的子類是用於解決${}的佔位符,好比本地變量 或者系統變量或者環境變量等。
As of Spring 3.1, PropertySourcesPlaceholderConfigurer should be used preferentially over this implementation; it is more flexible through taking advantage of the Environment and PropertySource mechanisms also made available in Spring 3.1.
從spring3.1開始,propertysourcesplaceholderconfigurer應該在這些實現中優先使用,經過利用Environment 和PropertySource機制使得其更靈活。
PropertyPlaceholderConfigurer is still appropriate(合適的) for use when:
Prior to Spring 3.1, the <context:property-placeholder/> namespace element registered an instance of PropertyPlaceholderConfigurer. It will still do so if using the spring-context-3.0.xsd definition of the namespace. That is, you can preserve registration of PropertyPlaceholderConfigurer through the namespace, even if using Spring 3.1; simply do not update your xsi:schemaLocation and continue using the 3.0 XSD.
在spring3.1以前<context:property-placeholder/> 命名空間註冊了一個PropertyPlaceholderConfigurer的實例,若是你使用spring-context-3.0.xsd 以前依然是能夠使用。
示例:例如一些配置文件中的常量爲了簡化不用頻繁的更改參數配置信息,spring3.0中提供了一種簡單的方式context:property-placeholder 元素。
只須要在spring的配置文件裏添加一句:<context:property-placeholder location=」」/>便可。這裏的location值爲參數配置文件的位置,參數配置文件與經常使用的參數配置文件相同,即鍵值對的形式。
PropertyPlaceholderConfiguration內置的功能很是豐富,若是未找到${xxx}中定義的xxx鍵,他還會去JVM系統屬性(System.getProperty())和環境變量(System.getenv())中尋找,經過啓用systemPropertiesMode和searchSystemEnviroment屬性,能夠控制這一行爲。
Since:
02.10.2003
Author:
Juergen Hoeller, Chris Beams
See Also:
setSystemPropertiesModeName(java.lang.String), PlaceholderConfigurerSupport, PropertyOverrideConfigurer, PropertySourcesPlaceholderConfigurer
Fields |
|
Modifier and Type |
Field and Description |
static int |
SYSTEM_PROPERTIES_MODE_FALLBACK Check system properties if not resolvable in the specified properties.檢查系統配置文件若是給定的配置文件沒有該屬性。 |
static int |
Never check system properties.從不檢查系統屬性。 |
static int |
SYSTEM_PROPERTIES_MODE_OVERRIDE Check system properties first, before trying the specified properties.在檢查特定的配置文件以前先檢查系統配置文件。 |
Constructors |
Constructor and Description |
All MethodsInstance MethodsConcrete Methods |
|
Modifier and Type |
Method and Description |
protected void |
processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) Visit each bean definition in the given bean factory and attempt to replace ${...} property placeholders with values from the given properties. 訪問bean工廠裏面定義的全部bean並試圖去使用給定的配置文件中的值去替換${}佔位符。 |
protected String |
resolvePlaceholder(String placeholder, Properties props) Resolve the given placeholder using the given properties.使用給定的配置文件替換佔位符。 |
protected String |
resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) Resolve the given placeholder using the given properties, performing a system properties check according to the given mode. |
protected String |
resolveSystemProperty(String key) Resolve the given key as JVM system property, and optionally also as system environment variable if no matching system property has been found. |
void |
setSearchSystemEnvironment(boolean searchSystemEnvironment) Set whether to search for a matching system environment variable if no matching system property has been found. |
void |
setSystemPropertiesMode(int systemPropertiesMode) Set how to check system properties: as fallback, as override, or never. |
void |
setSystemPropertiesModeName(String constantName) Set the system property mode by the name of the corresponding constant, e.g. |
public static final int SYSTEM_PROPERTIES_MODE_NEVER
Never check system properties.
See Also:
public static final int SYSTEM_PROPERTIES_MODE_FALLBACK
Check system properties if not resolvable in the specified properties. This is the default.
See Also:
public static final int SYSTEM_PROPERTIES_MODE_OVERRIDE
Check system properties first, before trying the specified properties. This allows system properties to override any other property source.
See Also:
public PropertyPlaceholderConfigurer()
public void setSystemPropertiesModeName(String constantName)
throws IllegalArgumentException
Set the system property mode by the name of the corresponding constant, e.g. "SYSTEM_PROPERTIES_MODE_OVERRIDE".
Parameters:
constantName - name of the constant
Throws:
IllegalArgumentException - if an invalid constant was specified
See Also:
public void setSystemPropertiesMode(int systemPropertiesMode)
Set how to check system properties: as fallback, as override, or never. For example, will resolve ${user.dir} to the "user.dir" system property.
The default is "fallback": If not being able to resolve a placeholder with the specified properties, a system property will be tried. "override" will check for a system property first, before trying the specified properties. "never" will not check system properties at all.
See Also:
SYSTEM_PROPERTIES_MODE_NEVER, SYSTEM_PROPERTIES_MODE_FALLBACK, SYSTEM_PROPERTIES_MODE_OVERRIDE, setSystemPropertiesModeName(java.lang.String)
public void setSearchSystemEnvironment(boolean searchSystemEnvironment)
Set whether to search for a matching system environment variable if no matching system property has been found. Only applied when "systemPropertyMode" is active (i.e. "fallback" or "override"), right after checking JVM system properties.
Default is "true". Switch this setting off to never resolve placeholders against system environment variables. Note that it is generally recommended to pass external values in as JVM system properties: This can easily be achieved in a startup script, even for existing environment variables.
NOTE: Access to environment variables does not work on the Sun VM 1.4, where the corresponding System.getenv(java.lang.String) support was disabled - before it eventually got re-enabled for the Sun VM 1.5. Please upgrade to 1.5 (or higher) if you intend to rely on the environment variable support.
See Also:
setSystemPropertiesMode(int), System.getProperty(String), System.getenv(String)
protected String resolvePlaceholder(String placeholder,Properties props,int systemPropertiesMode)
Resolve the given placeholder using the given properties, performing a system properties check according to the given mode.
The default implementation delegates to resolvePlaceholder (placeholder, props) before/after the system properties check.
Subclasses can override this for custom resolution strategies, including customized points for the system properties check.
Parameters:
placeholder - the placeholder to resolve
props - the merged properties of this configurer
systemPropertiesMode - the system properties mode, according to the constants in this class
Returns:
the resolved value, of null if none
See Also:
setSystemPropertiesMode(int), System.getProperty(java.lang.String), resolvePlaceholder(String, java.util.Properties)
protected String resolvePlaceholder(String placeholder, Properties props)
Resolve the given placeholder using the given properties. The default implementation simply checks for a corresponding property key.
Subclasses can override this for customized placeholder-to-key mappings or custom resolution strategies, possibly just using the given properties as fallback.
Note that system properties will still be checked before respectively after this method is invoked, according to the system properties mode.
Parameters:
placeholder - the placeholder to resolve
props - the merged properties of this configurer
Returns:
the resolved value, of null if none
See Also:
protected String resolveSystemProperty(String key)
Resolve the given key as JVM system property, and optionally also as system environment variable if no matching system property has been found.
Parameters:
key - the placeholder to resolve as system property key
Returns:
the system property value, or null if not found
See Also:
setSearchSystemEnvironment(boolean), System.getProperty(String), System.getenv(String)
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props)throws BeansException
Visit each bean definition in the given bean factory and attempt to replace ${...} property placeholders with values from the given properties.
Specified by:
processProperties in class PropertyResourceConfigurer
Parameters:
beanFactoryToProcess - the BeanFactory used by the application context
props - the Properties to apply
Throws:
BeansException - in case of errors