在做項(xiàng)目中涩哟,我們經(jīng)常會(huì)把項(xiàng)目使用到的常量放到一個(gè)或者多個(gè)Properties文件中番甩;項(xiàng)目啟動(dòng)加載Properties文件侵贵,程序會(huì)動(dòng)態(tài)的注入到相應(yīng)的屬性上或者動(dòng)態(tài)從Properties中獲取value。
如果使用Spring框架搭建的項(xiàng)目缘薛,Spring是如何處理Properties的窍育?
通過(guò)學(xué)習(xí)Spring和翻閱Spring源碼我了解到Spring對(duì)Properties的處理方式;接下來(lái)我會(huì)把學(xué)習(xí)的和從源碼看到的詳細(xì)介紹一下宴胧。
Configurer Properties
PropertyPlaceholderConfigurer
PropertySourcesPlaceholderConfigurer
Spring 對(duì)于Properties的操作都是分別基于上面兩個(gè)類漱抓,而且兩個(gè)類的實(shí)現(xiàn)方式是不一樣的。
PropertyPlaceholderConfigurer
這個(gè)類型是我在接觸Spring框架的時(shí)候就使用的類恕齐;但是當(dāng)時(shí)在用的時(shí)候沒(méi)有過(guò)多關(guān)注該類的實(shí)現(xiàn)方式乞娄,只停留在使用上;之后由于項(xiàng)目的功能需要我了解它的實(shí)現(xiàn)方式檐迟,并且寫了一篇文章專門介紹了該類补胚。
有興趣的可以先看看這篇文章:Spring Properties Loader
總結(jié)一下該類:
加載
- 指定的properties文件
- 合并指定的properties (參考:
PropertiesLoaderSupport.localProperties
)
解析
- props (上面兩種加載后的合并properties)
- System.getProperty()
- System.getenv()
具體如何查找值,請(qǐng)參考源碼追迟;
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#resolvePlaceholder(String, Properties, int)
PropertySourcesPlaceholderConfigurer
首先看看類結(jié)構(gòu):
通過(guò)對(duì)比兩個(gè)類的結(jié)構(gòu)發(fā)現(xiàn)溶其,除了類名的區(qū)別,還有就是PropertySourcesPlaceholderConfigurer
類實(shí)現(xiàn)EnvironmentAware
接口敦间,那么證明該類會(huì)有Environment
的操作瓶逃;
了解Spring的知道,如果是非web環(huán)境廓块,接口Environment
的實(shí)例類是StandardEnvironment
厢绝;web環(huán)境,接口Environment
的實(shí)例類是StandardServletEnvironment
带猴;而且實(shí)現(xiàn)類會(huì)初始化系統(tǒng)屬性System.getProperties
和環(huán)境變量System.getenv
到PropertySources中昔汉。
getSystemProperties()
和getSystemEnvironment()
兩個(gè)方法的內(nèi)容我就不粘出來(lái)了,自行去參考源碼拴清;
不同
兩個(gè)類加載的屬性有什么不同呢靶病?
PropertyPlaceholderConfigurer
這個(gè)類是把所有的屬性集中放到Properties中;
PropertySourcesPlaceholderConfigurer
該類有一個(gè)
PropertySources
的集合口予,集合中放的是PropertySource
娄周,它是一個(gè)抽象類,getProperty
方法交由子類實(shí)現(xiàn)沪停;每一個(gè)PropertySource
可以是一個(gè)Properties煤辨,而且PropertySource
可以是任何東西裳涛;例如:System.getProperteis
、System.getenv
而該類直接重寫了
postProcessBeanFactory
方法众辨,如果PropertySources
集合為空端三,此類會(huì)把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
因?yàn)榇祟愔袇R聚了
Environment
虚婿、多個(gè)PropertySource
;因?yàn)镋nvironment中加載了System.getProperteis
和System.getenv
泳挥,而且它們分別代表了一個(gè)PropertySource然痊;每個(gè)PropertySource會(huì)放到一個(gè)PropertySources集合中,每次取值時(shí)屉符,會(huì)根據(jù)位置順序剧浸,從每一個(gè)PropertySource中獲取屬性值;參考:StandardEnvironment#customPropertySources()
上面源碼中矗钟,我們看到此類把
Environment
作為一個(gè)PropertySource唆香,放到PropertySources集合中;而Environment
中也有一個(gè)PropertySources集合吨艇,此集合中默認(rèn)初始化的是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组贺;