【SpringBoot】Spring Boot 的配置加载机制
创始人
2024-02-10 18:59:53

简概

官网地址: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")。

envconfigprops两个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

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...