在做項目中,我們經常會把項目使用到的常量放到一個或者多個Properties文件中;項目啟動加載Properties文件,程序會動態的注入到相應的屬性上或者動態從Properties中獲取value。
如果使用Spring框架搭建的項目,Spring是如何處理Properties的?
通過學習Spring和翻閱Spring源碼我了解到Spring對Properties的處理方式;接下來我會把學習的和從源碼看到的詳細介紹一下。
Configurer Properties
PropertyPlaceholderConfigurer
PropertySourcesPlaceholderConfigurer
Spring 對于Properties的操作都是分別基于上面兩個類,而且兩個類的實現方式是不一樣的。
PropertyPlaceholderConfigurer
這個類型是我在接觸Spring框架的時候就使用的類;但是當時在用的時候沒有過多關注該類的實現方式,只停留在使用上;之后由于項目的功能需要我了解它的實現方式,并且寫了一篇文章專門介紹了該類。
有興趣的可以先看看這篇文章:Spring Properties Loader
總結一下該類:
加載
- 指定的properties文件
- 合并指定的properties (參考:
PropertiesLoaderSupport.localProperties
)
解析
- props (上面兩種加載后的合并properties)
- System.getProperty()
- System.getenv()
具體如何查找值,請參考源碼;
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#resolvePlaceholder(String, Properties, int)
PropertySourcesPlaceholderConfigurer
首先看看類結構:
通過對比兩個類的結構發現,除了類名的區別,還有就是PropertySourcesPlaceholderConfigurer
類實現EnvironmentAware
接口,那么證明該類會有Environment
的操作;
了解Spring的知道,如果是非web環境,接口Environment
的實例類是StandardEnvironment
;web環境,接口Environment
的實例類是StandardServletEnvironment
;而且實現類會初始化系統屬性System.getProperties
和環境變量System.getenv
到PropertySources中。
getSystemProperties()
和getSystemEnvironment()
兩個方法的內容我就不粘出來了,自行去參考源碼;
不同
兩個類加載的屬性有什么不同呢?
PropertyPlaceholderConfigurer
這個類是把所有的屬性集中放到Properties中;
PropertySourcesPlaceholderConfigurer
該類有一個
PropertySources
的集合,集合中放的是PropertySource
,它是一個抽象類,getProperty
方法交由子類實現;每一個PropertySource
可以是一個Properties,而且PropertySource
可以是任何東西;例如:System.getProperteis
、System.getenv
而該類直接重寫了
postProcessBeanFactory
方法,如果PropertySources
集合為空,此類會把Environment
、Properties文件
、localProperties
放到集合中;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (this.propertySources == null) {
this.propertySources = new MutablePropertySources();
if (this.environment != null) {
this.propertySources.addLast(
new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) {
@Override
public String getProperty(String key) {
return this.source.getProperty(key);
}
}
);
}
try {
PropertySource<?> localPropertySource =
new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties());
if (this.localOverride) {
this.propertySources.addFirst(localPropertySource);
}
else {
this.propertySources.addLast(localPropertySource);
}
}
catch (IOException ex) {
throw new BeanInitializationException("Could not load properties", ex);
}
}
processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
this.appliedPropertySources = this.propertySources;
}
取值
PropertyPlaceholderConfigurer
Properteis、System.getenv、System.getProperties
PropertySourcesPlaceholderConfigurer
因為此類中匯聚了
Environment
、多個PropertySource
;因為Environment中加載了System.getProperteis
和System.getenv
,而且它們分別代表了一個PropertySource;每個PropertySource會放到一個PropertySources集合中,每次取值時,會根據位置順序,從每一個PropertySource中獲取屬性值;參考:StandardEnvironment#customPropertySources()
上面源碼中,我們看到此類把
Environment
作為一個PropertySource,放到PropertySources集合中;而Environment
中也有一個PropertySources集合,此集合中默認初始化的是System.getProperteis
和System.getenv
;
private final MutablePropertySources propertySources = new MutablePropertySources(this.logger);
private final ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
@Override
public String getProperty(String key) {
return this.propertyResolver.getProperty(key);
}
@Override
public String getProperty(String key, String defaultValue) {
return this.propertyResolver.getProperty(key, defaultValue);
}
@Override
public <T> T getProperty(String key, Class<T> targetType) {
return this.propertyResolver.getProperty(key, targetType);
}
@Override
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
return this.propertyResolver.getProperty(key, targetType, defaultValue);
}
@Override
@Deprecated
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
return this.propertyResolver.getPropertyAsClass(key, targetType);
}
@Override
public String getRequiredProperty(String key) throws IllegalStateException {
return this.propertyResolver.getRequiredProperty(key);
}
@Override
public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
return this.propertyResolver.getRequiredProperty(key, targetType);
}
@Override
public String resolvePlaceholders(String text) {
return this.propertyResolver.resolvePlaceholders(text);
}
@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
return this.propertyResolver.resolveRequiredPlaceholders(text);
}
綜合我們講的和源碼,我們可以知道;此類匯聚了各種不用的PropertySource;
-
props
把properties文件、localProperties封裝成PropertySource;
-
Environment
有自己的PropertySources;