SpringBoot的配置优先级,一个具体的练习例子

简介:

SpringBoot里的官方文档叫做Externalized Configuration:

优先级依次如下:

(1) Devtools global settings properties in the $HOME/.config/spring-boot folder 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. 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.

(17) Default properties (specified by setting SpringApplication.setDefaultProperties).

做个实验,在run as configuration里,program argument设置为server.port=8001,

Environment环境变量设置为server.port=8002.

在SpringBoot项目内部的Application.properties文件设置成8000:

最后运行时,生效的端口是环境变量设置进去的8002:


在shell里使用set命令设置环境变量,也能按照期望的方式工作:


本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
2天前
|
IDE Java Maven
SpringBoot自定义starter及自动配置
SpringBoot自定义starter及自动配置
|
2天前
|
存储 Java 数据库
Spring Boot中如何配置和使用多数据源
Spring Boot中如何配置和使用多数据源
|
2天前
|
Java UED Spring
Spring Boot中的国际化配置
Spring Boot中的国际化配置
|
2天前
|
监控 Java 开发者
Spring Boot中的热部署配置
Spring Boot中的热部署配置
|
2天前
|
Java API Spring
Spring Boot中配置Swagger用于API文档
Spring Boot中配置Swagger用于API文档
|
2天前
|
缓存 监控 Java
Spring Boot中的缓存配置与优化
Spring Boot中的缓存配置与优化
|
2天前
|
监控 IDE Java
探索 IntelliJ IDEA 中 Spring Boot 运行配置选项及其作用
探索 IntelliJ IDEA 中 Spring Boot 运行配置选项及其作用
5 0
|
2天前
|
Java 数据管理 关系型数据库
Spring Boot中实现多数据源配置
Spring Boot中实现多数据源配置
|
2天前
|
自然语言处理 Java UED
Spring Boot中的国际化配置
Spring Boot中的国际化配置
|
2天前
|
监控 Java Spring
Spring Boot中的热部署配置
Spring Boot中的热部署配置