温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot的配置优先级是什么

发布时间:2021-12-29 16:21:15 来源:亿速云 阅读:296 作者:iii 栏目:开发技术

本篇内容主要讲解“SpringBoot的配置优先级是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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,

SpringBoot的配置优先级是什么

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

SpringBoot的配置优先级是什么

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

SpringBoot的配置优先级是什么

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

SpringBoot的配置优先级是什么SpringBoot的配置优先级是什么

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

SpringBoot的配置优先级是什么SpringBoot的配置优先级是什么

到此,相信大家对“SpringBoot的配置优先级是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI