侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

SpringBoot(8)之配置文件的加载顺序

zze
zze
2018-01-26 / 0 评论 / 0 点赞 / 663 阅读 / 3345 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

内部配置

通过前面的学习我们已经知道 SpringBoot 的配置文件有两种,application.propertiesapplication.yml,这两种配置文件是放在工程内的,所以在此我们将它归类为内部配置。而这些配置文件不仅仅只能放在 classpath 根目录下,且当它们放在不同的目录下时加载顺序是有顺序的,具体如下:

  1. ./config/application.properties
  2. ./config/application.yml
  3. ./application.properties
  4. ./application.yml
  5. classpath:config/application.properties
  6. classpath:config/application.yml
  7. classpath:application.properties
  8. classpath:application.yml

注(经测试部分 SpringBoot 版本下标 1-4 失效):

  1. 如果同一个目录下有 application.yml 也有 application.properties,默认先读取 application.properties
  2. 如果同一个配置属性,在多个配置文件都配置了,默认使用第 1 个读取到的,后面读取的不覆盖前面读取到的。
  3. 创建 SpringBoot 项目时,一般的配置文件放置在 classpath 根目录。

外部配置

SpringBoot 的配置文件不仅仅只能存在于工程内部,还可以存在于以下位置(摘自官方文档),优先级从高到低,高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置。

1、Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties 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、Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
6、ServletConfig init parameters.
7、ServletContext init parameters.
8、JNDI attributes from java:comp/env.
9、Java System properties (System.getProperties()).
10、OS environment variables.
11、A RandomValuePropertySource that has properties only in random.*.
12、Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
13、Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
14、Application properties outside of your packaged jar (application.properties and YAML variants).
15、Application properties packaged inside your jar (application.properties and YAML variants).
16、@PropertySource annotations on your @Configuration classes.
17、Default properties (specified by setting SpringApplication.setDefaultProperties).

下面是整理出来的可能会用到的方式:

1、命令行参数。
2、来自 java:comp/env 的 JNDI 属性。
3、java 系统属性(System.getProperties())。
4、操作系统环境变量。
5、RandomValuePropertySource 配置的 random.* 属性值。
# 优先加载带 profile 的配置
6、jar 包外部的 application-{profile}.properties 或 application.yml(带 spring.profile)的配置文件。
7、jar 包内部的 application-{profile}.properties 或 application.yml(带 spring.profile)的配置文件。
# 再来加载不带 profile 的配置
8、jar 包外部的 application.properties 或 application.yml(不带 spring.profile)的配置文件。
9、jar 包内部的 application.properties 或 application.yml(不带 spring.profile)的配置文件。
10、@Configuration 注解类上的 @PropertySource。
11、通过 SpringApplication.setDefaultProperties 指定的默认属性。

还可以通过 spring.config.location 属性来指定配置文件位置,不过这种方式需要是在项目打包以后通过命令行启动项目时来指定配置文件新位置,指定配置文件和默认加载的配置文件会一起起作用形成互补配置,例如: java -jar demo1-0.0.1-SNAPSHOT.jar --spring.config.location=F://application.properties。通过命令行方式运行 jar 文件不指定配置文件路径时默认也会加载和 jar 文件同级目录的配置文件。

0

评论区