溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot的配置優(yōu)先級是什么

發(fā)布時間:2021-12-29 16:21:15 來源:億速云 閱讀:286 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“SpringBoot的配置優(yōu)先級是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“SpringBoot的配置優(yōu)先級是什么”吧!

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

優(yōu)先級依次如下:

(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設(shè)置為server.port=8001,

SpringBoot的配置優(yōu)先級是什么

Environment環(huán)境變量設(shè)置為server.port=8002.

SpringBoot的配置優(yōu)先級是什么

在SpringBoot項目內(nèi)部的Application.properties文件設(shè)置成8000:

SpringBoot的配置優(yōu)先級是什么

最后運行時,生效的端口是環(huán)境變量設(shè)置進去的8002:

SpringBoot的配置優(yōu)先級是什么SpringBoot的配置優(yōu)先級是什么

在shell里使用set命令設(shè)置環(huán)境變量,也能按照期望的方式工作:

SpringBoot的配置優(yōu)先級是什么SpringBoot的配置優(yōu)先級是什么

到此,相信大家對“SpringBoot的配置優(yōu)先級是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI