SpringBoot配置文件你了解多少?

开发 前端
在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。

[[390859]]

环境:springboot2.2.13

SpringBoot 中有以下两种配置文件

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

接下来说下这两个配置文件有什么区别!

bootstrap/ application 的区别

bootstrap.yml(bootstrap.properties)先加载

application.yml(application.properties)后加载

bootstrap.yml 用于应用程序上下文的引导阶段,由父Spring ApplicationContext加载。父ApplicationContext 被加载到使用application.yml的之前。

在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖也就是说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:}。

加载制定环境的配置文件

一般我们都会为测试环境,开发环境,生产环境分别设置不同的配置文件,不同的环境加载不同的配置文件。

现有如下配置文件:

生成环境加载prod文件,测试环境加载test文件,开发环境加载dev文件,只需在application.yml配置文件中加入如下配置。

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

 这是在配置文件中写死,我们也可以在命令行制定

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

通过include包含多个配置文件,include是与环境无关的(也就是不管是什么环境都会加载)

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

 配置文件加载顺序

查看

ConfigFileApplicationListener.java源码,该文件中定义了从哪些地方加载配置文件。

加载顺序:

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

优先级从高到低,高的覆盖低的。

默认情况下加载的是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网卡配置Linux网卡配置

2020-04-09 10:49:19

VMware主机配置

2021-08-02 18:23:01

Spring隐私数据

2023-03-23 08:11:59

2020-03-25 08:47:22

智能边缘边缘计算网络

2023-10-25 08:17:06

Lite模式代理类

2012-12-27 10:58:24

KVMKVM概念

2023-10-29 08:35:47

AndroidAOP编程

2021-06-06 18:22:04

PprofGopher逻辑

2019-08-07 17:18:18

云计算云原生函数

2022-02-08 12:06:12

云计算

2023-09-07 10:26:50

接口测试自动化测试

2022-06-07 07:37:40

线程进程开发

2011-08-23 11:03:35

ATM

2011-02-21 13:14:29

Qmail

2015-11-09 10:44:37

DevOpsIT运维

2023-12-24 12:56:36

协程

2023-08-17 10:12:04

前端整洁架构

2020-12-10 09:00:00

开发.NET工具

2021-12-09 07:47:58

Flink 提交模式
点赞
收藏

51CTO技术栈公众号