前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot的配置优先级,一个具体的练习例子

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

作者头像
Jerry Wang
发布2020-02-19 15:45:05
5370
发布2020-02-19 15:45:05
举报

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命令设置环境变量,也能按照期望的方式工作:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档