偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

SpringBoot配置文件你了解多少?

開發(fā) 前端
在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應用程序的父上下文,也就是說 bootstrap 加載優(yōu)先于 applicaton。bootstrap 主要用于從額外的資源來加載配置信息,還可以在本地外部配置文件中解密屬性。

[[390859]]

環(huán)境:springboot2.2.13

SpringBoot 中有以下兩種配置文件

  • bootstrap (.yml 或者 .properties)
  • application (.yml 或者 .properties)

接下來說下這兩個配置文件有什么區(qū)別!

bootstrap/ application 的區(qū)別

bootstrap.yml(bootstrap.properties)先加載

application.yml(application.properties)后加載

bootstrap.yml 用于應用程序上下文的引導階段,由父Spring ApplicationContext加載。父ApplicationContext 被加載到使用application.yml的之前。

在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應用程序的父上下文,也就是說 bootstrap 加載優(yōu)先于 applicaton。bootstrap 主要用于從額外的資源來加載配置信息,還可以在本地外部配置文件中解密屬性。這兩個上下文共用一個環(huán)境,它是任何Spring應用程序的外部屬性的來源。bootstrap 里面的屬性會優(yōu)先加載,它們默認也不能被本地相同配置覆蓋也就是說bootstrap中的配置不能被覆蓋。

bootstrap/ application 的應用場景

application 配置文件這個容易理解,主要用于 Spring Boot 項目的自動化配置。

bootstrap 配置文件有以下幾個應用場景。

  • 使用 Spring Cloud Config 配置中心時,這時需要在 bootstrap 配置文件中添加連接到配置中心的配置屬性來加載外部配置中心的配置信息;
  • 一些固定的不能被覆蓋的屬性
  • 一些加密/解密的場景;

以下是來自官方對bootstrap.[yml/properties]的一個說明:

  1. A Spring Cloud application operates by creating a “bootstrap” context, which is a parent context for the main application. This context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files. The two contexts share an Environment, which is the source of external properties for any Spring application. By default, bootstrap properties (not bootstrap.properties but properties that are loaded during the bootstrap phase) are added with high precedence, so they cannot be overridden by local configuration. 
  2.  
  3. The bootstrap context uses a different convention for locating external configuration than the main application context. Instead of application.yml (or .properties), you can use bootstrap.yml, keeping the external configuration for bootstrap and main context nicely separate 

 自定義配置文件

  1. @Configuration 
  2. @ConfigurationProperties(prefix = "pack"
  3. @PropertySource(value = "classpath:config.properties"
  4. public class PackProperties { 
  5.   private String name
  6.   private Integer age ; 

  1. pack.name=xxxx 
  2. pack.age=10 

 注意:@PropertySource只能加載properties文件不能加載yml文件。

導入Spring XML文件

  1. @Configuration 
  2. @ImportResource(locations = {"classpath:application-jdbc.xml""classpath:application-aop.xml"}) 
  3. public class ResourceConfig { 

 application-jdbc.xml,application-aop.xml兩個配置文件必須是spring的配置文件。

配置文件默認值

有如下代碼:

  1. @Value("${pack.name}"
  2. private String name ; 

 當配置文件中沒有配置pack.name時,這時候服務啟動會報錯,這時候我們可以通過設置默認值來防止錯誤。

  1. @Value("${pack.name:xx}"
  2.     private String name ; 

 這里冒號 “:” 后面就是設置的默認值,這里也可以什么也不寫${pack.name:}。

加載制定環(huán)境的配置文件

一般我們都會為測試環(huán)境,開發(fā)環(huán)境,生產(chǎn)環(huán)境分別設置不同的配置文件,不同的環(huán)境加載不同的配置文件。

現(xiàn)有如下配置文件:

生成環(huán)境加載prod文件,測試環(huán)境加載test文件,開發(fā)環(huán)境加載dev文件,只需在application.yml配置文件中加入如下配置。

  1. spring: 
  2.   profiles: 
  3.     active: 
  4.     - dev 

 這是在配置文件中寫死,我們也可以在命令行制定

  1. java -jar xxxxx.jar --spring.profiles.active=dev 

通過include包含多個配置文件,include是與環(huán)境無關的(也就是不管是什么環(huán)境都會加載)

  1. spring: 
  2.   profiles: 
  3.     active: 
  4.     - dev 
  5.     include: 
  6.     - cloud 
  7.     - secret 

 配置文件加載順序

查看

ConfigFileApplicationListener.java源碼,該文件中定義了從哪些地方加載配置文件。

加載順序:

  • file:./config/
  • file:./config/*/
  • file:./
  • classpath:config/
  • classpath:

優(yōu)先級從高到低,高的覆蓋低的。

默認情況下加載的是application.yml或application.properties文件。

在啟動應用程序的時候可以制定配置文件的名稱

  1. java -jar xxxxx.jar --spring.config.name=myapp 

源碼:

通過代碼讀取配置文件

  1. @SpringBootApplication 
  2. public class SpringBootJwtApplication implements ApplicationRunner{ 
  3.  
  4.     public static void main(String[] args) { 
  5.         SpringApplication.run(SpringBootJwtApplication.class, args); 
  6.     } 
  7.     @Override 
  8.     public void run(ApplicationArguments args) throws Exception { 
  9.         ClassPathResource resource = new ClassPathResource("config.properties");     
  10.         try {         
  11.          Properties properties = PropertiesLoaderUtils.loadProperties(resource);              
  12.          System.out.println(properties) ;              
  13.         } catch (IOException e) {         
  14.             e.printStackTrace() ; 
  15.         } 
  16.     } 

 這里通過PropertiesLoaderUtils工具類來加載資源

完畢!!!

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2009-08-18 10:56:40

Linux網(wǎng)卡配置Linux網(wǎng)卡配置

2020-04-09 10:49:19

VMware主機配置

2020-03-25 08:47:22

智能邊緣邊緣計算網(wǎng)絡

2021-06-06 18:22:04

PprofGopher邏輯

2012-12-27 10:58:24

KVMKVM概念

2023-10-29 08:35:47

AndroidAOP編程

2023-10-25 08:17:06

Lite模式代理類

2022-06-07 07:37:40

線程進程開發(fā)

2022-02-08 12:06:12

云計算

2023-09-07 10:26:50

接口測試自動化測試

2011-08-23 11:03:35

ATM

2019-08-07 17:18:18

云計算云原生函數(shù)

2025-01-16 10:41:40

2015-11-09 10:44:37

DevOpsIT運維

2023-12-24 12:56:36

協(xié)程

2021-12-09 07:47:58

Flink 提交模式

2020-12-10 09:00:00

開發(fā).NET工具

2023-08-17 10:12:04

前端整潔架構

2023-03-23 08:11:59

2021-08-02 18:23:01

Spring隱私數(shù)據(jù)
點贊
收藏

51CTO技術棧公眾號