从01开始 从01开始
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)

peterjxl

人生如逆旅,我亦是行人
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)
  • JavaSE

  • JavaSenior

  • JavaEE

  • JavaWeb

  • Spring

  • 主流框架

  • SpringMVC

  • SpringBoot

    • SpringBoot教程-尚硅谷

      • SpringBoot课程介绍
      • Spring和SpringBoot
      • HelloWorld
      • 了解自动配置原理
      • 底层注解-@Configuration详解
      • 底层注解-@Import导入组件
      • 底层注解-@Conditional条件装配
      • 原生配置文件引入-@ImportResource
      • 底层注解-配置绑定@ConfigurationProperties
      • 自动配置原理
      • 自动配置流程
      • Lombok简化开发
      • DevTools
      • Spring-Initailizr
      • 配置文件-Yaml用法
      • Web开发简介
      • web开发-静态资源规则于定制化
      • 静态资源配置原理
      • Rest映射及源码解析
      • 请求映射原理
      • 常用参数注解使用
      • MatrixVariable:矩阵变量
      • 各种类型参数解析原理
      • Servlet-API参数解析原理
      • Model、Map参数解析原理
      • 自定义对象参数绑定原理
      • 自定义Converter原理
      • 数据响应原理
      • 内容协商原理
      • 基于请求参数的内容原理
      • 自定义MessageConverter原理
      • Thymeleaf初体验
      • web实验-后台管理系统
      • web实验-抽取公共页面
      • web实验-遍历数据
      • 源码分析-视图解析器与视图
      • 拦截器-登录检查与静态资源放行
      • 拦截器的执行时机和原理
      • 单文件和多文件上传的使用
      • 文件上传原理
      • 错误处理机制
      • 错误处理-底层组件源码分析
      • 异常处理流程
      • 几种异常处理原理
      • Web原生对象注入
      • 嵌入式Servlet容器
      • 定制化原理
      • 数据库场景的自动配置分析和整合测试
      • 自定义方式整合Druid
      • 通过starter整合Druid
      • 整合Mybatis
      • 使用注解整合Mybatis
      • 整合MybatisPlus操作数据库
      • MybatisPlus-列表分页展示
      • 整合Redis
      • 单元测试-Junit5
      • 单元测试-断言机制
      • 单元测试-前置条件
      • 单元测试-嵌套测试
      • 单元测试-参数化测试
      • 指标监控-基本概念
      • 指标监控-配置EndPoint
      • 指标监控-可视化
      • 原理解析-Profile功能
      • 配置文件深入
        • 外部配置源
        • 配置的优先级
      • 自定义Starter
      • SpringApplication初始化过程
      • SpringBoot完整启动过程
      • SpringBoot
  • Java并发

  • Java源码

  • JVM

  • 韩顺平

  • Java
  • Java
  • SpringBoot
  • SpringBoot教程-尚硅谷
2023-08-22
目录

配置文件深入

# 650.配置文件深入

讲讲更多关于配置文件的知识   ‍

# 外部配置源

SpringBoot文档中,有专门一节讲配置文件的:

4.2. Externalized Configuration

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments.

You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.

Property values can be injected directly into your beans by using the @Value​ annotation, accessed through Spring's Environment abstraction, or be bound to structured objects through @ConfigurationProperties​.

‍

大意:

  • SpringBoot允许使用配置文件,这样就可以让一套代码运行在不同的环境
  • 可以使用.properties,.yaml,环境变量 和 命令行的方式来指定配置
  • 配置文件的值可以通过@Value​注解直接注入bean中,也可以使用@ConfigurationProperties​将一系列配置绑定到对象中

‍

‍

例如,我们之前安装Java和Maven的时候,都设置过环境变量。我们可以在代码中取出Maven的环境变量:

@RestController
public class HelloController {
  
    @Value("${MAVEN_HOME}")
    private String mvn;
  
    @GetMapping("/mvn")
    public String getMvn() {
        return mvn;
    }
}
1
2
3
4
5
6
7
8
9
10
11

‍

效果:

​​

‍

‍

还可以遍历所有的环境变量,以及一些系统信息(例如Windows版本等):

@SpringBootApplication
public class LearnSpringBootProfileApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext run = SpringApplication.run(LearnSpringBootProfileApplication.class, args);

		ConfigurableEnvironment environment = run.getEnvironment();
		Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
		Map<String, Object> systemProperties = environment.getSystemProperties();
		System.out.println(systemEnvironment);
		System.out.println(systemProperties);
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13

‍

‍

运行结果:

​​

‍

‍

‍

# 配置的优先级

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

  1. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.
  2. @TestPropertySource annotations on your tests.
  3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  4. Command line arguments.
  5. .....

大意:SpringBoot对于加载配置有指定的顺序,并且允许覆盖,越下方的优先级越高(文档中列了17个,这里不全部举出),也不用全部记住,只需记得后面的配置会覆盖前面的配置即可

关于配置文件application.yaml,也是有加载顺序的:

  1. classpath 根路径(也就是resources目录下)
  2. classpath 根路径下config目录
  3. jar包当前目录下
  4. jar包当前目录的config目录
  5. /config子目录的直接子目录

也就是从上往下,逐个加载这些配置文件(如果没有则不加载)

‍

例如,在resources目录下新建config目录,然后新建application.yaml:

spring:
  profiles:
    active: test
1
2
3

‍

运行结果:test配置文件被加载

​

‍

‍

​

我们总结下加载文件的顺序:

  • 首先加载application.yaml文件,根据如下路径加载

    • classpath 根路径(也就是resources目录下)
    • classpath 根路径下config目录
    • jar包当前目录下
    • jar包当前目录的config目录
    • /config子目录的直接子目录
  • 然后加载指定的Profile

‍

由于后面加载的配置,会覆盖前面加载的配置,因此可以认为后面的配置优先级较高。

已将本文源码上传到 Gitee (opens new window) 和 GitHub (opens new window) 的分支demo2,读者可以通过切换分支来查看本文的示例代码

‍

‍

‍

‍

‍

在GitHub上编辑此页 (opens new window)
上次更新: 2023/8/23 10:10:57
原理解析-Profile功能
自定义Starter

← 原理解析-Profile功能 自定义Starter→

Theme by Vdoing | Copyright © 2022-2023 粤ICP备2022067627号-1 粤公网安备 44011302003646号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式