天天看点

「SpringBoot」Spring Boot 的配置加载机制

作者:嘟null

简概

官网地址:https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

Spring Boot允许您将配置外部化,这样您就可以在不同的环境中使用相同的应用程序代码。您可以使用各种外部配置源,包括Java properties文件、YAML文件、环境变量和命令行参数。

属性值可以通过使用@Value注解直接注入到bean中,通过Spring的Environment抽象访问,或者通过@ConfigurationProperties绑定到结构化对象。

Spring Boot使用非常特殊的PropertySource顺序,该顺序被设计成允许合理地覆盖值。后面的属性源可以覆盖前面属性源中定义的值。来源的考虑顺序如下:

  1. Default properties (specified by setting SpringApplication.setDefaultProperties).
  2. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
  3. Config data (such as application.properties files).
  4. A RandomValuePropertySource that has properties only in random.*.
  5. OS environment variables.
  6. Java System properties (System.getProperties()).
  7. JNDI attributes from java:comp/env.
  8. ServletContext init parameters.
  9. ServletConfig init parameters.
  10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  11. Command line arguments.
  12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  13. @TestPropertySource annotations on your tests.
  14. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

配置数据文件按以下顺序考虑:

  1. Application properties packaged inside your jar (application.properties and YAML variants).
  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  3. Application properties outside of your packaged jar (application.properties and YAML variants).
  4. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).

建议在整个应用程序中使用一种格式。如果在同一位置同时有.properties和.yml格式的配置文件,则.properties优先。

为了提供一个具体的例子,假设你开发了一个使用name属性的@Component,如下面的例子所示:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @Value("${name}")
    private String name;

    // ...

}           

在您的应用程序类路径上(例如,在您的jar中)可以有一个application.properties文件,该文件为name提供了合理的默认属性值。在新环境中运行时,application.properties文件可以在覆盖名称的jar之外提供。对于一次性测试,可以使用特定的命令行启动(例如,java -jar app.jar --name="Spring")。

env和configprops两个endpoint在确定属性为什么是某个值时非常有用。可以使用这两个endpoint来诊断意料之外的属性值。有关详细信息,请参阅“Production ready features”一节。

外化配置加载顺序

  • 开启 DevTools 时,~/.spring-boot-devtools.properties
  • 测试类上的 @TestPropertySource 注解
  • @SpringBootTest#properties 属性
  • 命令⾏参数( --server.port=9000 )
  • SPRING_APPLICATION_JSON 中的属性
  • ServletConfig 初始化参数
  • ServletContext 初始化参数
  • java:comp/env 中的 JNDI 属性
  • System.getProperties()
  • 操作系统环境变量
  • random.* 涉及到的 RandomValuePropertySource
  • jar 包外部的 application-{profile}.properties 或 .yml
  • jar 包内部的 application-{profile}.properties 或 .yml
  • jar 包外部的 application.properties 或 .yml
  • jar 包内部的 application.properties 或 .yml
  • @Configuration 类上的 @PropertySource
  • SpringApplication.setDefaultProperties() 设置的默认属性

application.properties

默认位置

  • ./config
  • ./
  • CLASSPATH 中的 /config
  • CLASSPATH 中的 /

修改名字或路径

  • spring.config.name
  • spring.config.location
  • spring.config.additional-location

Relaxed Binding

命名风格 使用范围 示例
短划线分隔

properties文件、

YAML文件、

系统属性

shuashua.spring-boot.first-demo
驼峰式 shuashua.springBoot.firstDemo
下划线分隔 shuashua.spring_boot.first_demo
全大写,下划线分隔 环境变量 SHUASHUA_SPRINGBOOT_FIRSTDEMO

继续阅读