SpringBoot的配置優先級,一個具體的練習例子

SpringBoot裏的官方文檔叫作Externalized Configuration:java

優先級依次以下:spring

(1) Devtools global settings properties in the $HOME/.config/spring-boot folder when devtools is active.shell

(2) @TestPropertySource annotations on your tests.app

(3) properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.dom

(4) Command line arguments.ide

(5) Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).spring-boot

(6) ServletConfig init parameters.spa

(7) ServletContext init parameters.server

(8) JNDI attributes from java:comp/env.blog

(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命令設置環境變量,也能按照指望的方式工做:


要獲取更多Jerry的原創文章,請關注公衆號"汪子熙":

相關文章
相關標籤/搜索