Spring 常用的兩種PropertyPlaceholderConfigurer

在做項目中,我們經常會把項目使用到的常量放到一個或者多個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

通過對比兩個類的結構發現,除了類名的區別,還有就是PropertySourcesPlaceholderConfigurer類實現EnvironmentAware接口,那么證明該類會有Environment的操作;

了解Spring的知道,如果是非web環境,接口Environment的實例類是StandardEnvironment;web環境,接口Environment的實例類是StandardServletEnvironment;而且實現類會初始化系統屬性System.getProperties和環境變量System.getenv到PropertySources中。

StandardEnvironment

getSystemProperties()getSystemEnvironment()兩個方法的內容我就不粘出來了,自行去參考源碼;

不同


兩個類加載的屬性有什么不同呢?

  • PropertyPlaceholderConfigurer

這個類是把所有的屬性集中放到Properties中;

  • PropertySourcesPlaceholderConfigurer

該類有一個PropertySources的集合,集合中放的是PropertySource,它是一個抽象類,getProperty方法交由子類實現;每一個PropertySource可以是一個Properties,而且PropertySource可以是任何東西;例如:System.getProperteisSystem.getenv

而該類直接重寫了postProcessBeanFactory方法,如果PropertySources集合為空,此類會把EnvironmentProperties文件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.getProperteisSystem.getenv,而且它們分別代表了一個PropertySource;每個PropertySource會放到一個PropertySources集合中,每次取值時,會根據位置順序,從每一個PropertySource中獲取屬性值;參考:StandardEnvironment#customPropertySources()

上面源碼中,我們看到此類把Environment作為一個PropertySource,放到PropertySources集合中;而Environment中也有一個PropertySources集合,此集合中默認初始化的是System.getProperteisSystem.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;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,925評論 6 342
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,740評論 18 399
  • 文章作者:Tyan博客:noahsnail.com | CSDN | 簡書 24. Externalized Co...
    SnailTyan閱讀 2,051評論 1 2
  • 身邊有個美女同學,才華橫溢得讓我垂涎三尺,結果就是我一時沖動報名加入30天寫作訓練營,幻想有朝一日自己哪怕能...
    邋遢睡神閱讀 248評論 0 2