欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot自定義配置屬性源(PropertySource)

 更新時間:2018年06月13日 14:46:47   作者:曹書豪  
這篇文章主要介紹了Spring Boot自定義配置屬性源(PropertySource),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

配置覆蓋優(yōu)于profile

在生產(chǎn)實踐中,配置覆蓋是解決不同環(huán)境不同配置的常用方法。比如用生產(chǎn)服務(wù)器上的配置文件覆蓋包內(nèi)的文件,或者使用中心化的配置服務(wù)來覆蓋默認的業(yè)務(wù)配置。

相比于profile機制(比如maven的profile、spring boot的profile-specific properties),即不同環(huán)境使用不同的配置文件,覆蓋的方式更有優(yōu)勢。程序員在開發(fā)時不需要關(guān)心生產(chǎn)環(huán)境數(shù)據(jù)庫的地址、賬號等信息,一次構(gòu)建即可在不同環(huán)境中運行,而profile機制需要將生產(chǎn)環(huán)境的配置寫到項目資源文件中,而且要為不同環(huán)境使用不同的構(gòu)建參數(shù)或者運行參數(shù)。

Spring提供了靈活的配置擴展能力,有多種方式將自定義的屬性源,將集成進來,可以輕松地實現(xiàn)配置覆蓋。

本文基于Spring Boot 1.4.8/Spring 4.3.12編寫

使用@PropertySource注解實現(xiàn)自定義配置文件和配置覆蓋

@ConfigurationProperties
@Configuration
public class DemoProperties {
  // properties with getter/setters
}
@PropertySource(value = {
    "test.properties",
    "file:/etc/test.properties",
},
    ignoreResourceNotFound = true)
@Configuration
public class DemoAutoConfiguration {

  @Autowired
  private DemoProperties demoProperties;

  @PostConstruct
  public void init() {
    System.out.println(demoProperties);
  }
}

Spring支持使用PropertySource注解引入自定義配置文件,其中"test.properties"將使Spring從classpath下加載該文件,"file:/etc/test.properties"將使Spring從文件系統(tǒng)加載/etc/test.properties文件,ignoreResourceNotFound = true使Spring忽略文件加載失敗的異常,即配置文件是可選的。

同時,由于"file:/etc/test.properties"位于"test.properties"之后,這使得文件系統(tǒng)的配置文件可以覆蓋classpath下的配置。

自定義屬性源工廠

如果想要更加靈活的自定義屬性源,比如實現(xiàn)從中心化的配置服務(wù)加載配置,可以通過實現(xiàn)PropertySourceFactory接口,并通過配置PropertySource注解的factory參數(shù)來實現(xiàn)。

@Configuration
@PropertySource(value = ""/*placeholder*/,
    factory = CompositePropertySourceFactory.class)
public class CompositeConfigAutoConfiguration {
}

value字段用于指定配置源對應(yīng)的資源文件,如果不需要使用資源文件,可以配置為任意值,參數(shù)值將會被傳遞到factory參數(shù)的createPropertySource方法。

如果ignoreResourceNotFound字段指定為true,那么factory拋出的異常將被忽略,否則將導(dǎo)致啟動失敗。有的時候,直接把啟動失敗暴露出來不失為一種好的做法。

PropertySourceFactory接口的定義如下:

/**
 * Strategy interface for creating resource-based {@link PropertySource} wrappers.
 *
 * @author Juergen Hoeller
 * @since 4.3
 * @see DefaultPropertySourceFactory
 */
public interface PropertySourceFactory {

 /**
 * Create a {@link PropertySource} that wraps the given resource.
 * @param name the name of the property source
 * @param resource the resource (potentially encoded) to wrap
 * @return the new {@link PropertySource} (never {@code null})
 * @throws IOException if resource resolution failed
 */
 PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException;

}

需要注意的是PropertySourceFactory的加載時機早于Spring Beans容器,因此實現(xiàn)上不能依賴于Spring的IOC。

PropertySourceFactory要求實現(xiàn)類返回PropertySource。PropertySource是Spring屬性(或者說配置)功能的核心接口,有很多實現(xiàn),比如:

  1. ResourcePropertySource 從Resource加載PropertySource
  2. PropertiesPropertySource 從properties文件加載PropertySource
  3. SystemEnvironmentPropertySource 從系統(tǒng)環(huán)境變量加載PropertySource
  4. MapPropertySource 包裝一個Map為PropertySource(Adapter模塊)
  5. CompositePropertySource 支持將若干PropertySource進行組合(Composite模式)

實際實現(xiàn)類遠不如這些,具體的可以閱讀Spring文檔或源碼。

在自定義屬性源時比較常用的是MapPropertySource和CompositePropertySource。

MapPropertySource可以用于將自己加載的屬性數(shù)據(jù)包裝,參考其構(gòu)造方法。

public MapPropertySource(String name, Map<String, Object> source) {
 super(name, source);
}

后者可以通過組合裝載多個屬性源并自定義覆蓋順序。例如:

PropertySource<?> packageInsidePropertySource = packageInsidePropertySourceIterateLoader.loadPropertySource(compositePropertySource);
compositePropertySource.addPropertySource(packageInsidePropertySource);

PropertySource<?> outerFilePropertySource = outerFilePropertySourceIterateLoader.loadPropertySource(compositePropertySource);
// 優(yōu)先級高于前者
compositePropertySource.addFirstPropertySource(outerFilePropertySource);

addFirstPropertySource方法可以設(shè)置傳入的PropertySource為最高優(yōu)先級(在此CompositePropertySource內(nèi)部),addPropertySource方法則相反,放在后面的優(yōu)先級更低。

加載依賴jar包中所有同名配置文件

直接從classpath加載配置文件,要求文件必須存在于classpath中??紤]在WEB項目中,如果文件存在于某個依賴的jar包中,即位于WEB-INF/lib/xxx.jar中,此時基于classpath無法直接加載。此時可以使用Spring提供的PathMatchingResourcePatternResolver,按資源名稱掃描所有jar包來實現(xiàn)目的。

private List<Resource> getPackageInsideResourcesByPattern(String resourceName) throws IOException {
 String resourcePathPattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + HbootConfigConstants.CONFIGS + resourceName;
 ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
 return Arrays.asList(resourcePatternResolver.getResources(resourcePathPattern));
}

然后就可以使用ResourcePropertySource從Resource構(gòu)建PropertySource傳給Spring。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • DoytoQuery中的關(guān)聯(lián)查詢方案示例詳解

    DoytoQuery中的關(guān)聯(lián)查詢方案示例詳解

    這篇文章主要為大家介紹了DoytoQuery中的關(guān)聯(lián)查詢方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 老生常談Java網(wǎng)絡(luò)編程TCP通信(必看篇)

    老生常談Java網(wǎng)絡(luò)編程TCP通信(必看篇)

    下面小編就為大家?guī)硪黄仙U凧ava網(wǎng)絡(luò)編程TCP通信(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • java中單雙斜杠的使用圖文詳解

    java中單雙斜杠的使用圖文詳解

    JAVA中的斜杠有正斜杠與反斜杠之分,正斜杠,一般就叫做斜杠,下面這篇文章主要給大家介紹了關(guān)于java中單雙斜杠使用的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • SpringBoot集成swagger的實例代碼

    SpringBoot集成swagger的實例代碼

    Swagger 是一款RESTFUL接口的文檔在線自動生成+功能測試功能軟件,這篇文章主要介紹了SpringBoot集成swagger,需要的朋友可以參考下
    2017-12-12
  • SpringBoot整合JWT實戰(zhàn)教程

    SpringBoot整合JWT實戰(zhàn)教程

    JWT(JSON?Web?Token)是一種用于身份驗證和授權(quán)的開放標準(RFC?7519),它使用JSON格式傳輸信息,可以在不同系統(tǒng)之間安全地傳遞數(shù)據(jù),這篇文章主要介紹了SpringBoot整合JWT實戰(zhàn)教程,需要的朋友可以參考下
    2023-06-06
  • SpringBoot配置默認HikariCP數(shù)據(jù)源

    SpringBoot配置默認HikariCP數(shù)據(jù)源

    咱們開發(fā)項目的過程中用到很多的開源數(shù)據(jù)庫鏈接池,比如druid、c3p0、BoneCP等等,本文主要介紹了SpringBoot配置默認HikariCP數(shù)據(jù)源,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • java中Map接口常用的方法解讀

    java中Map接口常用的方法解讀

    這篇文章主要介紹了java中Map接口常用的方法解讀,Map接口是雙列集合,它的每一個元素都包含一個鍵對象key和值對象Value,鍵和值對象之間存在一種對應(yīng)關(guān)系,稱為映射,需要的朋友可以參考下
    2024-01-01
  • 詳解MyBatis Plus中分頁插件的使用

    詳解MyBatis Plus中分頁插件的使用

    這篇文章主要為大家詳細介紹了MyBatis Plus中分頁插件使用的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下
    2023-02-02
  • javaWeb用戶權(quán)限控制簡單實現(xiàn)過程

    javaWeb用戶權(quán)限控制簡單實現(xiàn)過程

    這篇文章主要為大家詳細介紹了javaWeb用戶權(quán)限控制簡單實現(xiàn)過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 一文帶你全面了解Java?Hashtable

    一文帶你全面了解Java?Hashtable

    HashTable是jdk?1.0中引入的產(chǎn)物,基本上現(xiàn)在很少使用了,但是會在面試中經(jīng)常被問到。本文就來帶大家一起深入了解一下Hashtable,需要的可以參考一下
    2022-09-09

最新評論