關于spring讀取配置的順序和讀取配置的位置沿猜,開發(fā)中大多不關注枚荣,故此忽略,如有興趣啼肩,請參看源文檔橄妆。
源文檔地址:Externalized Configuration
1. Configuring Random Values
隨機配置,基于RandomValuePropertySource 祈坠,在配置文件里可以寫隨機值
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]}
2. Accessing Command Line Properties
這點告訴我們可以接受命令行參數(shù)作為屬性害碾,會覆蓋aplication配置里的屬性。
比如啟動命令加上 --server.port=9000
3. Application Property Files
Spring加載配置文件的位置赦拘,按順序從以下四個次序:
- A /config subdirectory of the current directory
- The current directory
- A classpath /config package
- The classpath root
可以更改配置文件地址慌随,名稱,不過意義不大躺同。有興趣可以看源文檔阁猜。
4. Profile-specific Properties
不同環(huán)境應用不同的配置,通過 application-{profile}.properties蹋艺,應該都很熟悉了剃袍。
目前大多數(shù)項目都使用配置中心實現(xiàn)這個功能,了解一下可以了捎谨。
5. Placeholders in Properties
就想下面這樣民效,用占位符取屬性。
app.name=MyApp
app.description=${app.name} is a Spring Boot application
6. Encrypting Properties
spring沒有提供加密屬性的方法涛救,但是提供了 EnvironmentPostProcessor 接口研铆,可以在啟動完成前環(huán)境準備之后更改屬性。這個接口顯然不能通過bean的方式配置州叠,需要通過SPI的方式加載。
7.Using YAML Instead of Properties
可以使用yaml作為配置文件凶赁,這個也司空見慣了咧栗,基本都是這樣子用的。
8. Type-safe Configuration Properties
即通過@ConfigurationProperties 直接注入一組屬性到配置類中虱肄,比起@Value注解致板,對于層次化的屬性來說更為友好。無論做架構還是業(yè)務代碼都很常用咏窿,重點了解斟或。
- @ConfigurationProperties并不會被spring掃描配置成bean,若想托管到spring集嵌,請使用-@Component或者@ConfigurationPropertiesScan
- 使用構造器綁定@ConstructorBinding萝挤,可以創(chuàng)建不可變配置御毅。
- 可以配合@EnableConfigurationProperties,把配置屬性與配置類@Configuration綁定怜珍。
- 以下是配置類和對應配置文件的綁定
@ConfigurationProperties("acme")
@Component
public class AcmeProperties {
@Getter
@Setter
private boolean enabled;
@Getter
@Setter
private InetAddress remoteAddress;
@Getter
private final Security security = new Security();
@Getter
@Setter
public static class Security {
private String username;
private String password;
private List<String> roles = new ArrayList<>(Collections.singleton("USER"));
}
}
# application.yml
acme:
enabled:true
remote-address: 192.168.1.1
security:
username: admin
roles:
- USER
- ADMIN
# additional configuration as required
- Third-party Configuration
可以使用@Bean注解在配置bean的時候給三方類注入屬性
@ConfigurationProperties(prefix = "another")
@Bean
public AnotherComponent anotherComponent() {
...
}
- Relaxed Binding 寬松綁定
@ConfigurationProperties(prefix="acme.my-project.person")
public class OwnerProperties {
private String firstName;
}
屬性綁定并不十分嚴格端蛆,支持各種風格的寫法。
對于上面這個類酥泛,一下四種都可以注入 firstName今豆。
//推薦 .properties .yml
acme.my-project.person.first-name
//傳統(tǒng) camel
acme.myProject.person.firstName
acme.my_project.person.first_name
//推薦系統(tǒng)環(huán)境變量的寫法
ACME_MYPROJECT_PERSON_FIRSTNAME
不同屬性源的綁定規(guī)則:
Property Source | Simple | List |
---|---|---|
Properties Files | a_b 或者 a-b 或者 aB | a.b[1]或者, |
YAML Files | a_b 或者 a-b 或者 aB | -或者, |
Environment Variables | A_B | A_B_1 |
System properties | a_b 或者 a-b 或者 aB | a.b[1]或者柔袁, |
Map綁定:使用[]才會保留所有字符呆躲,否則會去掉特殊字符
acme:
map:
"[/key1]": value1 => /key1
"[/key2]": value2 => /key2
/key3: value3 => key3
- Merging Complex Types
對于List,Map類型捶索,重復定義的屬性合并的規(guī)則插掂。
PS:開發(fā)時請盡量不要重復,具體想了解規(guī)則請查閱源文檔情组。 - Properties Conversion
屬性類型轉化燥筷,可以基于Converters 注冊,也有其他的解決方案院崇。
spring 支持以下這三種類型的屬性轉化:
java.time.Duration 持續(xù)時間
java.time.Period 周期
org.springframework.util.unit.DataSize 數(shù)據(jù)大小
具體想了解可以查閱源文檔肆氓,用的不多。 - @ConfigurationProperties Validation
屬性配置校驗,直接使用@Validated底瓣,例子如下谢揪。用的很多了,不在贅述捐凭。
@ConfigurationProperties(prefix="acme")
@Validated
public class AcmeProperties {
@NotNull
private InetAddress remoteAddress;
@Valid
private final Security security = new Security();
// ... getters and setters
public static class Security {
@NotEmpty
public String username;
// ... getters and setters
}
}
- @ConfigurationProperties vs. @Value
兩種屬性配置的對比:
Feature | @ConfigurationProperties |
@Value |
---|---|---|
Relaxed binding | Yes | Limited (see note below) |
Meta-data support | Yes | No |
SpEL evaluation |
No | Yes |
ps:關于 Meta-data,原文介紹如下:
Spring Boot jars include metadata files that provide details of all supported configuration properties. The files are designed to let IDE developers offer contextual help and “code completion” as users are working with application.properties or application.yml files.
只有ide開發(fā)人員才會獲取meta data拨扶,基礎框架建設者可以提供ide識別的json數(shù)據(jù),普通程序員還是洗洗睡吧茁肠。