SpringBoot基礎系列-SpringBoot配置
原創作品,可以轉載,但是請標註出處地址:ofollow,noindex" target="_blank">https://www.cnblogs.com/V1haoge/p/9990680.html
SpringBoot基礎系列-SpringBoot配置
概述
屬性配置方式:
- properties檔案
- yml檔案
- 環境變數
- 命令列引數
屬性值的使用方式:
- @Value("${propertyKey}")註解獲取
- 從Environment中獲取
-
使用@ConfigurationProperties繫結到Bean
具體配置
隨機值配置
適用型別:
- integers
- longs
- uuids
-
strings
my.secret=${random.value} my.number=${random.int} my.bignumber=${random.long} my.uuid=${random.uuid} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]}
說明:rendom.value結果是strings;random.int*結果是integers;random.long結果是longs;random.uuid結果是uuids
命令列屬性
命令列屬性會被轉換成為property,而儲存到Environment之中,而且優先順序極高,一般是在最後進行儲存,如果有相同的屬性會進行覆蓋。
application.properties
配置檔案
格式:
- *.properties
- *.yml
application.properties
屬性檔案會被SpringBoot應用自動載入,而且有一個載入順序:
- 當前目錄的/config子目錄下
- 當前目錄下
- classpath目錄的/config子目錄下
-
classpath目錄下
上面的排列順序從上到下是按照優先順序從高到低排列,而實際上我們一般使用都在classpath目錄下
通過Environment屬性spring.config.name
我們可以自定義applicaiton.properties
檔案的名稱,通過Environment屬性spring.config.location
自定義applicaiton.properties
檔案的位置。這兩個配置要在應用啟用之前配置,所以需要將其配置到系統環境變數或者系統引數或者命令列引數中優先讀取。
java -jar xxx.jar --spring.config.name=myAppConfig java -jar xxx.jar --spring.config.location=classpath:custon-config/,file:./custon-config/ java -jar xxx.jar --spring.config.additional-location=classpath:custon-config/,file:./custon-config/
上面將其定義為命令列引數。其中後兩個配置是不同的,spring.config.location會覆蓋預設的搜尋路徑,spring.config.additional-location不會覆蓋預設的搜尋路徑
application-{profile}.properties
配置檔案
我們可以在applicaiton.prperties所在目錄定義applicaiton-{profile}.properties配置檔案作為某個profile的專屬配置檔案,只有在該profile處於active狀態時才會讀取。
如果在application.properties和application-{profile}.properties中定義的相同名稱的配置內容,後者會覆蓋前者。
屬性佔位符
我們可以在屬性配置時使用佔位符,動態的使用其他屬性的值:
name=weiyihaoge desc=${name} is a good man.
使用YMAL檔案替換properties
YMAL的依賴包SnakeYAML會被Spring-boot-starter自動載入。
無論是YMAL還是properties,只要被載入到記憶體,其實都會設定到environment之中,這時我們使用@Value("${propertyKey}")就能獲取到屬性的值,該註解其實是在從environment中獲取值。
YMAL配置檔案除了配置格式不同於properties之外,配置方式基本相同。下面主要看看幾個不同之處:
Multi-profile YMAL
使用properties配置檔案時,不同的profile需要定義不同的配置檔案,但是使用YMAL配置檔案時,我們可以在一個YMAL檔案中定義所有的profile配置。
server: port: 8080 --- spring: profiles: dev server: port: 8081 --- spring: profiles: test server: port: 8082 --- spring: profiles: pro server: port: 8083
@PropertySource
YMAL配置內容無法通過@PropertySource註解載入,如果要使用該註解載入配置內容,只能使用properties配置檔案。
@PropertySource註解一般是用於載入自定義的屬性配置檔案的,因為如果是預設的配置檔案application.properties或者application.yml都會被自動載入,根本用不到這個註解,也只有自定義的配置檔案需要這個註解單獨進行載入,而該註解只能用於properties配置檔案,那麼我們就有一個原則:不要自定義YMAL檔案,凡是自定義的配置檔案全部使用properties檔案,而預設的配置完全可以採用application.yml,使用YMAL的優勢。
型別安全的配置屬性
所謂型別安全的配置屬性即我們可以將自定義的配置內容直接對應到一個配置類中,在應用啟動後生成一個配置Bean供程式使用。
這一般在配置屬性比較多的情況下使用,因為這種情況下使用@Value有些過於麻煩。
使用方法:
第一步:新增自定義配置資料
可以在預設的配置檔案application.yml中新增,也可以在自定義的配置檔案中新增(如果自定義配置檔案,一定要定義成properties檔案)
在application.yml中新增配置內容
#屬性對映測試 app: name: springdemo size: 100M user: weiyihaoge version: 0.0.1
在myConfig.properties中新增配置內容
app.name=springdemo2 app.size=50M app.user=ahaha app.version=1.0.0
定義承接屬性的Bean類
針對application.yml中定義的屬性
@Data @NoArgsConstructor @AllArgsConstructor @ConfigurationProperties("app") @Configuration public class AppProperty { private String name; private String size; private String user; private String version; }
針對自定義myConfig.properties中定義的屬性
@Data @NoArgsConstructor @AllArgsConstructor @ConfigurationProperties("app") @Configuration @PropertySource("classpath:/config/myConfig.properties") public class AppProperty { private String name; private String size; private String user; private String version; }
使用
@Controller @RequestMapping("base") @Log4j2 @Api(hidden = true) public class Base { @Autowired private AppProperty property; @RequestMapping(value = "/getProperties",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "獲取配置屬性", httpMethod = "GET") public String getProperty(){ return property.toString(); } }
執行結果
瀏覽器執行以下請求:
http:127.0.0.1:8080/base/getProperties
預設配置檔案的結果
AppProperty(name=springdemo, size=100M, user=weiyihaoge, version=0.0.1)
自定義配置檔案的結果
AppProperty(name=springdemo2, size=50M, user=ahaha, version=1.0.0)
注意:如果在預設的配置檔案和自定義配置檔案中配置了同樣的內容,那麼自定義的內容將不會被對映,預設的配置檔案中配置的資訊會優先被對映。