在日常開發(fā)中我們有一些配置是不能寫死在代碼里的廉白,因為他們有變化的可能。比如服務地址乖菱,dev坡锡,test,pre 窒所,product 每個環(huán)境的地址肯定都是不一致的鹉勒。而這些變量我們怎么來處理呢。就是將其定義到properties文件中吵取。
配置定義
spring boot 支持2種類型的配置文件禽额。
properties
properties為鍵值對的配置方式,沒有層級關系皮官,一個key 對應一個value脯倒;
示例
spring.application.name=hello-world
spring.application.index=${random.int}
yml
yml是一種有層級結構的書寫配置文件的方式
示例
spring:
application:
name: hello-world
index: ${random.int}
雖然yml看上去優(yōu)雅一點,但是如果層級關系過多,可讀性下降的會比較快捺氢。注意使用yml配置 “:” 后邊必須有一個空格.
配置取值
value注解
@Value(value = "${spring.application.name}")
@Value(value = "${spring.application.index:0}")
@Value(value = "#{'${spring.application.name}'.length()}")
${spring.application.name}
這種配置方式要求程序在啟動時必須能獲取到這個配置信息藻丢。一般是properties或者yml文件中必須要有該配置,當然其他配置位置也行摄乒。
${spring.application.index:0}
相對于上面的配置方式就是給屬性設置了一個默認值悠反,即:后面的值,這樣程序啟動就不會報錯缺狠,而是使用默認值问慎。使用該方式要一定要小心,因為有將默認值得發(fā)布至生產環(huán)境的可能挤茄。
#{'${spring.application.name}'.length()}
#后面是一個表達式如叼,可以給屬性注入一個表達式的結果。
ConfigurationProperties注解
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.application")
public class ApplicationConfig {
private String name;
private int index;
private int appNameLength;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getAppNameLength(){
return this.name.length();
}
}
@ConfigurationProperties(prefix = "spring.application")
用這個注解能抽象出一個類穷劈。完成屬性的注入笼恰。這樣在需要使用該配置時使用@Autowired
注解獲得該對象踊沸。
ps:這兩種方式都能完成資源的注入那我們怎么選擇呢?我的建議是社证,如果這個配置僅在1個類中使用時使用Value注解進行逼龟,如果這個配置在多個類中使用使用ConfigurationProperties注解。保證如果要變更1個配置名只修改1個位置追葡。減少出錯的可能腺律。
各個環(huán)境激活
上面說過我們把這些信息抽象成配置就是因為這些東西可能隨著環(huán)境不同的變化而變化。那我們怎么選擇適當的環(huán)境呢宜肉。
首先我們根據不同的環(huán)境建立多個配置文件匀钧。如圖
以dev為例
spring.application.name=hello-world-dev
spring.application.index=${random.int}
里面配置的是該環(huán)境下的配置信息。我們在啟動程序的時候使用啟動參數來決定使用那套配置谬返。
java -jar hello-wrold-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
如果我們本地調試要怎么切換環(huán)境呢之斯。
1.在文件中 application.properties 增加 spring.profiles.active=dev 配置
2.修改idea Run/Debug Configurations 如圖
如果使用2方法,還可以設置VM options 來進行設置遣铝。但是設置方式是
-Dspring.profiles.active=dev
而不是--如果對應到啟動命令為
java -jar -Dspring.profiles.active=dev hello-wrold-0.0.1-SNAPSHOT.jar
因為設置的是jvm 的參數所有要跟在java
后面佑刷。
隨機數配置
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]}
配置獲取方式及其優(yōu)先級
1.Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
2.@TestPropertySource annotations on your tests.
3.@SpringBootTest#properties annotation attribute on your tests.
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.
17.Default properties (specified by setting SpringApplication.setDefaultProperties).
優(yōu)先級是由高到低。其實這中間大多數我們都不常用酿炸,在日常工作中最主要的是4.命令行參數瘫絮,10系統(tǒng)環(huán)境變量。 12.外部application-{profile}.properties配置文件梁沧,13.application-{profile}.properties 內部配置文件檀何。
結果演示
示例代碼:https://github.com/tong467/hello-wrold
參考文獻:https://docs.spring.io/spring-boot/docs/2.0.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-random-values