配置覆蓋優于profile
在生產實踐中,配置覆蓋是解決不同環境不同配置的常用方法。比如用生產服務器上的配置文件覆蓋包內的文件,或者使用中心化的配置服務來覆蓋默認的業務配置。
相比于profile機制(比如maven的profile、spring boot的profile-specific properties),即不同環境使用不同的配置文件,覆蓋的方式更有優勢。程序員在開發時不需要關心生產環境數據庫的地址、賬號等信息,一次構建即可在不同環境中運行,而profile機制需要將生產環境的配置寫到項目資源文件中,而且要為不同環境使用不同的構建參數或者運行參數。
spring提供了靈活的配置擴展能力,有多種方式將自定義的屬性源,將集成進來,可以輕松地實現配置覆蓋。
本文基于spring boot 1.4.8/spring 4.3.12編寫
使用@propertysource注解實現自定義配置文件和配置覆蓋
1
2
3
4
5
|
@configurationproperties @configuration public class demoproperties { // properties with getter/setters } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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從文件系統加載/etc/test.properties文件,ignoreresourcenotfound = true使spring忽略文件加載失敗的異常,即配置文件是可選的。
同時,由于"file:/etc/test.properties"位于"test.properties"之后,這使得文件系統的配置文件可以覆蓋classpath下的配置。
自定義屬性源工廠
如果想要更加靈活的自定義屬性源,比如實現從中心化的配置服務加載配置,可以通過實現propertysourcefactory接口,并通過配置propertysource注解的factory參數來實現。
1
2
3
4
5
|
@configuration @propertysource (value = "" /*placeholder*/ , factory = compositepropertysourcefactory. class ) public class compositeconfigautoconfiguration { } |
value字段用于指定配置源對應的資源文件,如果不需要使用資源文件,可以配置為任意值,參數值將會被傳遞到factory參數的createpropertysource方法。
如果ignoreresourcenotfound字段指定為true,那么factory拋出的異常將被忽略,否則將導致啟動失敗。有的時候,直接把啟動失敗暴露出來不失為一種好的做法。
propertysourcefactory接口的定義如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * 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容器,因此實現上不能依賴于spring的ioc。
propertysourcefactory要求實現類返回propertysource。propertysource是spring屬性(或者說配置)功能的核心接口,有很多實現,比如:
- resourcepropertysource 從resource加載propertysource
- propertiespropertysource 從properties文件加載propertysource
- systemenvironmentpropertysource 從系統環境變量加載propertysource
- mappropertysource 包裝一個map為propertysource(adapter模塊)
- compositepropertysource 支持將若干propertysource進行組合(composite模式)
實際實現類遠不如這些,具體的可以閱讀spring文檔或源碼。
在自定義屬性源時比較常用的是mappropertysource和compositepropertysource。
mappropertysource可以用于將自己加載的屬性數據包裝,參考其構造方法。
1
2
3
|
public mappropertysource(string name, map<string, object> source) { super (name, source); } |
后者可以通過組合裝載多個屬性源并自定義覆蓋順序。例如:
1
2
3
4
5
6
|
propertysource<?> packageinsidepropertysource = packageinsidepropertysourceiterateloader.loadpropertysource(compositepropertysource); compositepropertysource.addpropertysource(packageinsidepropertysource); propertysource<?> outerfilepropertysource = outerfilepropertysourceiterateloader.loadpropertysource(compositepropertysource); // 優先級高于前者 compositepropertysource.addfirstpropertysource(outerfilepropertysource); |
addfirstpropertysource方法可以設置傳入的propertysource為最高優先級(在此compositepropertysource內部),addpropertysource方法則相反,放在后面的優先級更低。
加載依賴jar包中所有同名配置文件
直接從classpath加載配置文件,要求文件必須存在于classpath中。考慮在web項目中,如果文件存在于某個依賴的jar包中,即位于web-inf/lib/xxx.jar中,此時基于classpath無法直接加載。此時可以使用spring提供的pathmatchingresourcepatternresolver,按資源名稱掃描所有jar包來實現目的。
1
2
3
4
5
|
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構建propertysource傳給spring。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.caosh.me/be-tech/spring-boot-custom-properties/