成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringBoot配置文件你了解多少?

開發(fā) 前端
在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應(yīng)用程序的父上下文,也就是說 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 用于應(yīng)用程序上下文的引導(dǎo)階段,由父Spring ApplicationContext加載。父ApplicationContext 被加載到使用application.yml的之前。

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

bootstrap/ application 的應(yīng)用場景

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

bootstrap 配置文件有以下幾個應(yīng)用場景。

  • 使用 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文件。

導(dǎo)入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時,這時候服務(wù)啟動會報錯,這時候我們可以通過設(shè)置默認值來防止錯誤。

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

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

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

一般我們都會為測試環(huán)境,開發(fā)環(huán)境,生產(chǎn)環(huán)境分別設(shè)置不同的配置文件,不同的環(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)境無關(guā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文件。

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

  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工具類來加載資源

完畢!!!

 

責任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2009-08-18 10:56:40

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

2020-04-09 10:49:19

VMware主機配置

2023-10-25 08:17:06

Lite模式代理類

2023-10-29 08:35:47

AndroidAOP編程

2012-12-27 10:58:24

KVMKVM概念

2021-06-06 18:22:04

PprofGopher邏輯

2020-03-25 08:47:22

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

2020-12-10 09:00:00

開發(fā).NET工具

2015-11-09 10:44:37

DevOpsIT運維

2023-12-24 12:56:36

協(xié)程

2021-12-09 07:47:58

Flink 提交模式

2023-08-17 10:12:04

前端整潔架構(gòu)

2022-06-07 07:37:40

線程進程開發(fā)

2023-09-07 10:26:50

接口測試自動化測試

2022-02-08 12:06:12

云計算

2019-08-07 17:18:18

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

2025-01-16 10:41:40

2011-08-23 11:03:35

ATM

2023-03-23 08:11:59

2021-08-02 18:23:01

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

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 在线观看免费毛片 | 91视视频在线观看入口直接观看 | 91视频在线网站 | 日韩和的一区二区 | 日本免费一区二区三区视频 | 亚洲成人免费网址 | 午夜小视频在线播放 | 精品九九 | 亚洲欧美日韩电影 | 中文字幕在线观看第一页 | 色综合一区二区 | 精品视频在线免费观看 | 中文字幕在线一区 | 亚洲一区二区视频在线观看 | 欧美中文字幕 | 精品视频在线观看 | 亚州激情 | 97精品超碰一区二区三区 | 日日日日操 | www.99热| 久久久www成人免费无遮挡大片 | 欧美成人激情 | 日本久久黄色 | 暖暖日本在线视频 | gogo肉体亚洲高清在线视 | 欧美亚洲视频 | 青青草精品 | 国产成人亚洲精品 | 狠狠影院| 久久国 | 中文字幕亚洲精品在线观看 | 日韩人体在线 | 中文字幕一区在线观看视频 | 亚洲精品久久久久久久久久久 | www4虎| 九九热这里只有精品在线观看 | 欧美久操网| 欧美视频在线看 | 日韩精品免费在线观看 | 亚洲欧美在线观看视频 | 欧美日韩电影一区二区 |