SpringBoot核心配置文件bootstrap與application用法
Spring Boot 中有以下兩種配置文件
- bootstrap (.yml 或者 .properties)
- application (.yml 或者 .properties)
1、SpringBoot bootstrap配置文件不生效問題
單獨(dú)使用SpringBoot,發(fā)現(xiàn)其中的bootstrap.properties文件無法生效,改成yaml格式也無濟(jì)于事。
最后調(diào)查發(fā)現(xiàn)原來是因?yàn)镾pringBoot本身并不支持,需要和Spring Cloud 的組件結(jié)合——只有加上Spring Cloud Context依賴才能生效。
即在pom中引入:
<!--需要引入該jar才能使bootstrap配置文件生效--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </dependency>
2、bootstrap/ application 的區(qū)別
在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應(yīng)用程序的父上下文,也就是說 bootstrap 加載優(yōu)先于 applicaton。
bootstrap 主要用于從額外的資源來加載配置信息,還可以在本地外部配置文件中解密屬性。
這兩個(gè)上下文共用一個(gè)環(huán)境,它是任何Spring應(yīng)用程序的外部屬性的來源。bootstrap 里面的屬性會優(yōu)先加載,它們默認(rèn)也不能被本地相同配置覆蓋。
因此,對比 application 配置文件,bootstrap 配置文件具有以下幾個(gè)特性。
- boostrap 由父 ApplicationContext 加載,比 applicaton 優(yōu)先加載
- boostrap 里面的屬性不能被覆蓋
3、bootstrap/ application 的應(yīng)用場景
application 配置文件這個(gè)容易理解,主要用于 Spring Boot 項(xiàng)目的自動化配置。
bootstrap 配置文件有以下幾個(gè)應(yīng)用場景。
- 使用 Spring Cloud Config 配置中心時(shí),這時(shí)需要在 bootstrap 配置文件中添加連接到配置中心的配置屬性來加載外部配置中心的配置信息;
- 一些固定的不能被覆蓋的屬性
- 一些加密/解密的場景;
技術(shù)上,bootstrap.yml 是被一個(gè)父級的 Spring ApplicationContext 加載的。這個(gè)父級的 Spring ApplicationContext是先加載的,在加載application.yml 的 ApplicationContext之前。
為何需要把 config server 的信息放在 bootstrap.yml 里?
當(dāng)使用 Spring Cloud 的時(shí)候,配置信息一般是從 config server 加載的,為了取得配置信息(比如密碼等),你需要一些提早的引導(dǎo)配置。因此,把 config server 信息放在 bootstrap.yml,用來加載在這個(gè)時(shí)期真正需要的配置信息。
4、高級使用場景
4.1 啟動上下文
Spring Cloud會創(chuàng)建一個(gè)Bootstrap Context
,作為Spring應(yīng)用的Application Context
的父上下文。
初始化的時(shí)候,Bootstrap Context
負(fù)責(zé)從外部源加載配置屬性并解析配置。這兩個(gè)上下文共享一個(gè)從外部獲取的Environment
。
Bootstrap
屬性有高優(yōu)先級,默認(rèn)情況下,它們不會被本地配置覆蓋。
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
來禁用bootstrap
。
4.2 應(yīng)用上下文層次結(jié)構(gòu)
如果你通過SpringApplication
或者SpringApplicationBuilder
創(chuàng)建一個(gè)Application Context
,那么會為spring應(yīng)用的Application Context
創(chuàng)建父上下文Bootstrap Context
。
在Spring里有個(gè)特性,子上下文會繼承父類的property sources
and profiles
,所以main application context
相對于沒有使用Spring Cloud Config,會新增額外的property sources
。
額外的property sources
有:
- “bootstrap” : 如果在Bootstrap Context掃描到PropertySourceLocator并且有屬性,則會添加到CompositePropertySource。Spirng Cloud Config就是通過這種方式來添加的屬性的,詳細(xì)看源碼ConfigServicePropertySourceLocator`。下面也也有一個(gè)例子自定義的例子。
- “applicationConfig: [classpath:bootstrap.yml]” ,(如果有spring.profiles.active=production則例如 applicationConfig: [classpath:/bootstrap.yml]#production): 如果你使用bootstrap.yml來配置Bootstrap Context,他比application.yml優(yōu)先級要低。它將添加到子上下文,作為Spring Boot應(yīng)用程序的一部分。下文有介紹。
由于優(yōu)先級規(guī)則,Bootstrap Context不包含從bootstrap.yml來的數(shù)據(jù),但是可以用它作為默認(rèn)設(shè)置。
你可以很容易的擴(kuò)展任何你建立的上下文層次,可以使用它提供的接口,或者使用SpringApplicationBuilder包含的方法(parent(),child(),sibling())。
Bootstrap Context將是最高級別的父類。擴(kuò)展的每一個(gè)Context都有有自己的bootstrap property source(有可能是空的)。
擴(kuò)展的每一個(gè)Context都有不同spring.application.name。同一層層次的父子上下文原則上也有一有不同的名稱,因此,也會有不同的Config Server配置。
子上下文的屬性在相同名字的情況下將覆蓋父上下文的屬性。
- 注意SpringApplicationBuilder允許共享Environment到所有層次,但是不是默認(rèn)的。
- 因此,同級的兄弟上下文不在和父類共享一些東西的時(shí)候不一定有相同的profiles或者property sources。
4.3 修改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來配置引導(dǎo)ApplicationContext。
如果有一個(gè)激活的profile(來源于spring.profiles.active或者Environment的Api構(gòu)建),例如bootstrap-development.properties 就是配置了profile為development的配置文件.
4.3.1 覆蓋遠(yuǎn)程屬性
property sources被bootstrap context 添加到應(yīng)用通常通過遠(yuǎn)程的方式,比如”Config Server”。
默認(rèn)情況下,本地的配置文件不能覆蓋遠(yuǎn)程配置,但是可以通過啟動命令行參數(shù)來覆蓋遠(yuǎn)程配置。
如果需要本地文件覆蓋遠(yuǎn)程文件,需要在遠(yuǎn)程配置文件里
4.3.2 設(shè)置授權(quán)
spring.cloud.config.allowOverride=true(這個(gè)配置不能在本地被設(shè)置)。
一旦設(shè)置了這個(gè)權(quán)限,你可以配置更加細(xì)粒度的配置來配置覆蓋的方式,
比如:
- spring.cloud.config.overrideNone=true 覆蓋任何本地屬性
- spring.cloud.config.overrideSystemProperties=false 僅僅系統(tǒng)屬性和環(huán)境變量
源文件見PropertySourceBootstrapProperties.
4.4 自定義啟動配置
bootstrap context是依賴/META-INF/spring.factories文件里面的org.springframework.cloud.bootstrap.BootstrapConfiguration條目下面,通過逗號分隔的Spring @Configuration類來建立的配置。
任何main application context需要的自動注入的Bean可以在這里通過這種方式來獲取。
這也是ApplicationContextInitializer建立@Bean的方式。
可以通過@Order來更改初始化序列,默認(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都會在SpingApplicatin啟動前加入到它的上下文里去。
4.5 自定義引導(dǎo)配置來源:Bootstrap Property Sources
默認(rèn)的property source
添加額外的配置是通過配置服務(wù)(Config Server),你也可以自定義添加property source
通過實(shí)現(xiàn)PropertySourceLocator
接口來添加。
你可以使用它加配置屬性從不同的服務(wù)、數(shù)據(jù)庫、或者其他。
下面是一個(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文件:
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
那么,”customProperty“的PropertySource將會被包含到應(yīng)用。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC中@Valid不起效BindingResult讀取不到Error信息
在寫SpringMVC項(xiàng)目時(shí),由于要對表單數(shù)據(jù)進(jìn)行校驗(yàn),需要使用@Valid進(jìn)行校驗(yàn),但是在進(jìn)行數(shù)據(jù)校驗(yàn)時(shí),BindingResult對象無法攔截非法表單數(shù)據(jù),result.hasErrors()無論怎么輸入都會返回false,本文詳細(xì)的介紹一下解決方法2021-09-09springboot的統(tǒng)一異常處理,使用@RestControllerAdvice詳解
@RestControllerAdvice是Spring Boot中的全局異常處理注解,結(jié)合了@ControllerAdvice和@ResponseBody的功能,通過創(chuàng)建自定義異常類和全局異常處理器,可以實(shí)現(xiàn)統(tǒng)一異常處理,確保API的一致性和響應(yīng)的標(biāo)準(zhǔn)化2024-12-12java獲取百度網(wǎng)盤真實(shí)下載鏈接的方法
這篇文章主要介紹了java獲取百度網(wǎng)盤真實(shí)下載鏈接的方法,涉及java針對URL操作及頁面分析的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07springboot集成spark并使用spark-sql的示例詳解
這篇文章主要介紹了spring-boot集成spark并使用spark-sql的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02java開發(fā)使用StringUtils.split避坑詳解
這篇文章主要為大家介紹了java開發(fā)使用StringUtils.split避坑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11SpringBoot集成Milvus實(shí)現(xiàn)數(shù)據(jù)增刪改查功能
milvus支持的語言比較多,支持python, Java, Go,node等開發(fā)語言,本文主要介紹如何使用Java語言,采用springboot框架集成和調(diào)用Milvus數(shù)據(jù)庫,這篇文章主要介紹了SpringBoot集成Milvus,實(shí)現(xiàn)數(shù)據(jù)增刪改查,需要的朋友可以參考下2025-04-04