SpringBoot默認(rèn)支持properties和YAML兩種格式的配置文件。前者格式簡(jiǎn)單,但是只支持鍵值對(duì)。如果需要表達(dá)列表,最好使用YAML格式。SpringBoot支持自動(dòng)加載約定名稱的配置文件,例如application.yml。如果是自定義名稱的配置文件,就要另找方法了??上У氖牵幌袂罢哂蠤PropertySource這樣方便的加載方式,后者的加載必須借助編碼邏輯來(lái)實(shí)現(xiàn)。
一、bootstrap.yml / bootstrap.properties與application.yml / application.properties執(zhí)行順序
bootstrap.yml(bootstrap.properties)用來(lái)在程序引導(dǎo)時(shí)執(zhí)行,應(yīng)用于更加早期配置信息讀取,如可以使用來(lái)配置application.yml中使用到參數(shù)等
application.yml(application.properties) 應(yīng)用程序特有配置信息,可以用來(lái)配置后續(xù)各個(gè)模塊中需使用的公共參數(shù)等。
bootstrap.yml 先于 application.yml 加載
二、典型的應(yīng)用場(chǎng)景
- 當(dāng)使用 Spring Cloud Config Server 的時(shí)候,你應(yīng)該在 bootstrap.yml 里面指定 spring.application.name 和 spring.cloud.config.server.git.uri
- 和一些加密/解密的信息
技術(shù)上,bootstrap.yml 是被一個(gè)父級(jí)的 Spring ApplicationContext 加載的。這個(gè)父級(jí)的 Spring ApplicationContext是先加載的,在加載application.yml 的 ApplicationContext之前。
為何需要把 config server 的信息放在 bootstrap.yml 里?
當(dāng)使用 Spring Cloud 的時(shí)候,配置信息一般是從 config server 加載的,為了取得配置信息(比如密碼等),你需要一些提早的引導(dǎo)配置。因此,把 config server 信息放在 bootstrap.yml,用來(lái)加載在這個(gè)時(shí)期真正需要的配置信息。
三、高級(jí)使用場(chǎng)景
啟動(dòng)上下文
Spring Cloud會(huì)創(chuàng)建一個(gè)Bootstrap Context
,作為Spring應(yīng)用的Application Context
的父上下文。初始化的時(shí)候,Bootstrap Context
負(fù)責(zé)從外部源加載配置屬性并解析配置。這兩個(gè)上下文共享一個(gè)從外部獲取的Environment
。Bootstrap
屬性有高優(yōu)先級(jí),默認(rèn)情況下,它們不會(huì)被本地配置覆蓋。 Bootstrap context
和Application Context
有著不同的約定,所以新增了一個(gè)bootstrap.yml
文件,而不是使用application.yml
(或者application.properties
)。保證Bootstrap Context
和Application Context
配置的分離。下面是一個(gè)例子: bootstrap.yml
spring:
application:
name: foo
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:8888}
推薦在bootstrap.yml
or application.yml
里面配置spring.application.name
. 你可以通過設(shè)置spring.cloud.bootstrap.enabled=false
來(lái)禁用bootstrap
。
應(yīng)用上下文層次結(jié)構(gòu)
如果你通過SpringApplication
或者SpringApplicationBuilder
創(chuàng)建一個(gè)Application Context
,那么會(huì)為spring應(yīng)用的Application Context
創(chuàng)建父上下文Bootstrap Context
。在Spring里有個(gè)特性,子上下文會(huì)繼承父類的property sources
and profiles
,所以main application context
相對(duì)于沒有使用Spring Cloud Config,會(huì)新增額外的property sources
。額外的property sources
有:
- “bootstrap” : 如果在Bootstrap Context掃描到PropertySourceLocator并且有屬性,則會(huì)添加到CompositePropertySource。Spirng Cloud Config就是通過這種方式來(lái)添加的屬性的,詳細(xì)看源碼ConfigServicePropertySourceLocator`。下面也也有一個(gè)例子自定義的例子。
- “applicationConfig: [classpath:bootstrap.yml]” ,(如果有spring.profiles.active=production則例如 applicationConfig: [classpath:/bootstrap.yml]#production): 如果你使用bootstrap.yml來(lái)配置Bootstrap Context,他比application.yml優(yōu)先級(jí)要低。它將添加到子上下文,作為Spring Boot應(yīng)用程序的一部分。下文有介紹。
由于優(yōu)先級(jí)規(guī)則,Bootstrap Context不包含從bootstrap.yml來(lái)的數(shù)據(jù),但是可以用它作為默認(rèn)設(shè)置。
你可以很容易的擴(kuò)展任何你建立的上下文層次,可以使用它提供的接口,或者使用SpringApplicationBuilder包含的方法(parent(),child(),sibling())。Bootstrap Context將是最高級(jí)別的父類。擴(kuò)展的每一個(gè)Context都有有自己的bootstrap property source(有可能是空的)。擴(kuò)展的每一個(gè)Context都有不同spring.application.name。同一層層次的父子上下文原則上也有一有不同的名稱,因此,也會(huì)有不同的Config Server配置。子上下文的屬性在相同名字的情況下將覆蓋父上下文的屬性。
注意SpringApplicationBuilder允許共享Environment到所有層次,但是不是默認(rèn)的。因此,同級(jí)的兄弟上下文不在和父類共享一些東西的時(shí)候不一定有相同的profiles或者property sources。
修改Bootstrap屬性配置
源碼位置BootstrapApplicationListener。
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
String configLocation = environment.resolvePlaceholders("${spring.cloud.bootstrap.location:}");
Map<String, Object> bootstrapMap = new HashMap<>();bootstrapMap.put("spring.config.name",configName);
if(StringUtils.hasText(configLocation)){
bootstrapMap.put("spring.config.location", configLocation);
}
bootstrap.yml是由spring.cloud.bootstrap.name(默認(rèn):”bootstrap”)或者spring.cloud.bootstrap.location(默認(rèn)空)。這些屬性行為與spring.config.*類似,通過它的Environment來(lái)配置引導(dǎo)ApplicationContext。如果有一個(gè)激活的profile(來(lái)源于spring.profiles.active或者Environment的Api構(gòu)建),例如bootstrap-development.properties 就是配置了profile為development的配置文件.
覆蓋遠(yuǎn)程屬性
property sources被bootstrap context 添加到應(yīng)用通常通過遠(yuǎn)程的方式,比如”Config Server”。默認(rèn)情況下,本地的配置文件不能覆蓋遠(yuǎn)程配置,但是可以通過啟動(dòng)命令行參數(shù)來(lái)覆蓋遠(yuǎn)程配置。如果需要本地文件覆蓋遠(yuǎn)程文件,需要在遠(yuǎn)程配置文件里設(shè)置授權(quán)
spring.cloud.config.allowOverride=true(這個(gè)配置不能在本地被設(shè)置)。一旦設(shè)置了這個(gè)權(quán)限,你可以配置更加細(xì)粒度的配置來(lái)配置覆蓋的方式,
比如:
- spring.cloud.config.overrideNone=true 覆蓋任何本地屬性
- spring.cloud.config.overrideSystemProperties=false 僅僅系統(tǒng)屬性和環(huán)境變量
源文件見PropertySourceBootstrapProperties
自定義啟動(dòng)配置
bootstrap context是依賴/META-INF/spring.factories文件里面的org.springframework.cloud.bootstrap.BootstrapConfiguration條目下面,通過逗號(hào)分隔的Spring @Configuration類來(lái)建立的配置。任何main application context需要的自動(dòng)注入的Bean可以在這里通過這種方式來(lái)獲取。這也是ApplicationContextInitializer建立@Bean的方式??梢酝ㄟ^@Order來(lái)更改初始化序列,默認(rèn)是”last”。
# spring-cloud-context-1.1.1.RELEASE.jar
# spring.factories
# AutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\
org.springframework.cloud.autoconfigure.RefreshAutoConfiguration,\
org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration,\
org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.bootstrap.BootstrapApplicationListener,\
org.springframework.cloud.context.restart.RestartListener
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\
org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\
org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
警告
小心,你添加的自定義BootstrapConfiguration類沒有錯(cuò)誤的@ComponentScanned到你的主應(yīng)用上下文,他們可能是不需要的。使用一個(gè)另外的包不被@ComponentScan或者@SpringBootApplication注解覆蓋到。
bootstrap context通過spring.factories配置的類初始化的所有的Bean都會(huì)在SpingApplicatin啟動(dòng)前加入到它的上下文里去。
自定義引導(dǎo)配置來(lái)源:Bootstrap Property Sources
默認(rèn)的property source
添加額外的配置是通過配置服務(wù)(Config Server),你也可以自定義添加property source
通過實(shí)現(xiàn)PropertySourceLocator
接口來(lái)添加。你可以使用它加配置屬性從不同的服務(wù)、數(shù)據(jù)庫(kù)、或者其他。
下面是一個(gè)自定義的例子:
@Configuration
public class CustomPropertySourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
return new MapPropertySource("customProperty",
Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended"));
}
}
Environment被ApplicationContext建立,并傳入property sources(可能不同個(gè)profile有不同的屬性),所以,你可以從Environment尋找找一些特別的屬性。比如spring.application.name,它是默認(rèn)的Config Server property source。
如果你建立了一個(gè)jar包,里面添加了一個(gè)META-INF/spring.factories文件:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-414110.html
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
那么,”customProperty“的PropertySource將會(huì)被包含到應(yīng)用。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-414110.html
到了這里,關(guān)于SpringBoot 常用的配置文件 application.yml和 bootstrap.yml的區(qū)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!