SpringBoot2.0新特性之配置綁定全解析
在Spring Boot 2.0中推出了Relaxed Binding 2.0,對(duì)原有的屬性綁定功能做了非常多的改進(jìn)以幫助我們更容易的在Spring應(yīng)用中加載和讀取配置信息。下面本文就來(lái)說說Spring Boot 2.0中對(duì)配置的改進(jìn)。
配置文件綁定
簡(jiǎn)單類型
在Spring Boot 2.0中對(duì)配置屬性加載的時(shí)候會(huì)除了像1.x版本時(shí)候那樣移除特殊字符外,還會(huì)將配置均以全小寫的方式進(jìn)行匹配和加載。所以,下面的4種配置方式都是等價(jià)的:
properties格式:
spring.jpa.databaseplatform=mysql spring.jpa.database-platform=mysql spring.jpa.databasePlatform=mysql spring.JPA.database_platform=mysql
yaml格式:
spring: jpa: databaseplatform: mysql database-platform: mysql databasePlatform: mysql database_platform: mysql
Tips:推薦使用全小寫配合-分隔符的方式來(lái)配置,比如:spring.jpa.database-platform=mysql
List類型
在properties文件中使用[]來(lái)定位列表類型,比如:
spring.my-example.url[0]=http://example.com spring.my-example.url[1]=http://spring.io
也支持使用逗號(hào)分割的配置方式,上面與下面的配置是等價(jià)的:
spring.my-example.url=http://example.com,http://spring.io
而在yaml文件中使用可以使用如下配置:
spring: my-example: url: - http://example.com - http://spring.io
也支持逗號(hào)分割的方式:
spring: my-example: url: http://example.com, http://spring.io
注意:在Spring Boot 2.0中對(duì)于List類型的配置必須是連續(xù)的,不然會(huì)拋出UnboundConfigurationPropertiesException異常,所以如下配置是不允許的:
foo[0]=a foo[2]=b
在Spring Boot 1.x中上述配置是可以的,foo[1]由于沒有配置,它的值會(huì)是null
Map類型
Map類型在properties和yaml中的標(biāo)準(zhǔn)配置方式如下:
properties格式:
spring.my-example.foo=bar spring.my-example.hello=world
yaml格式:
spring: my-example: foo: bar hello: world
注意:如果Map類型的key包含非字母數(shù)字和-的字符,需要用[]括起來(lái),比如:
spring: my-example: '[foo.baz]': bar
環(huán)境屬性綁定
簡(jiǎn)單類型
在環(huán)境變量中通過小寫轉(zhuǎn)換與.替換_來(lái)映射配置文件中的內(nèi)容,比如:環(huán)境變量SPRING_JPA_DATABASEPLATFORM=mysql的配置會(huì)產(chǎn)生與在配置文件中設(shè)置spring.jpa.databaseplatform=mysql一樣的效果。
List類型
由于環(huán)境變量中無(wú)法使用[和]符號(hào),所以使用_來(lái)替代。任何由下劃線包圍的數(shù)字都會(huì)被認(rèn)為是[]的數(shù)組形式。比如:
MY_FOO_1_ = my.foo[1] MY_FOO_1_BAR = my.foo[1].bar MY_FOO_1_2_ = my.foo[1][2]
另外,最后環(huán)境變量最后是以數(shù)字和下劃線結(jié)尾的話,最后的下劃線可以省略,比如上面例子中的第一條和第三條等價(jià)于下面的配置:
MY_FOO_1 = my.foo[1] MY_FOO_1_2 = my.foo[1][2]
系統(tǒng)屬性綁定
簡(jiǎn)單類型
系統(tǒng)屬性與文件配置中的類似,都以移除特殊字符并轉(zhuǎn)化小寫后實(shí)現(xiàn)綁定,比如下面的命令行參數(shù)都會(huì)實(shí)現(xiàn)配置spring.jpa.databaseplatform=mysql的效果:
-Dspring.jpa.database-platform=mysql -Dspring.jpa.databasePlatform=mysql -Dspring.JPA.database_platform=mysql
List類型
系統(tǒng)屬性的綁定也與文件屬性的綁定類似,通過[]來(lái)標(biāo)示,比如:
-D"spring.my-example.url[0]=http://example.com" -D"spring.my-example.url[1]=http://spring.io"
同樣的,他也支持逗號(hào)分割的方式,比如:
-Dspring.my-example.url=http://example.com,http://spring.io
屬性的讀取
上文介紹了Spring Boot 2.0中對(duì)屬性綁定的內(nèi)容,可以看到對(duì)于一個(gè)屬性我們可以有多種不同的表達(dá),但是如果我們要在Spring應(yīng)用程序的environment中讀取屬性的時(shí)候,每個(gè)屬性的唯一名稱符合如下規(guī)則:
- 通過.分離各個(gè)元素
- 最后一個(gè).將前綴與屬性名稱分開
- 必須是字母(a-z)和數(shù)字(0-9)
- 必須是小寫字母
- 用連字符-來(lái)分隔單詞
- 唯一允許的其他字符是[和],用于List的索引
- 不能以數(shù)字開頭
所以,如果我們要讀取配置文件中spring.jpa.database-platform的配置,可以這樣寫:
this.environment.containsProperty("spring.jpa.database-platform")
而下面的方式是無(wú)法獲取到spring.jpa.database-platform配置內(nèi)容的:
this.environment.containsProperty("spring.jpa.databasePlatform")
注意:使用@Value獲取配置內(nèi)容的時(shí)候也需要這樣的特點(diǎn)
全新的綁定API
在Spring Boot 2.0中增加了新的綁定API來(lái)幫助我們更容易的獲取配置信息。下面舉個(gè)例子來(lái)幫助大家更容易的理解:
例子一:簡(jiǎn)單類型
假設(shè)在propertes配置中有這樣一個(gè)配置:com.didispace.foo=bar
我們?yōu)樗鼊?chuàng)建對(duì)應(yīng)的配置類:
@Data
@ConfigurationProperties(prefix = "com.didispace")
public class FooProperties {
private String foo;
}
接下來(lái),通過最新的Binder就可以這樣來(lái)拿配置信息了:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
Binder binder = Binder.get(context.getEnvironment());
// 綁定簡(jiǎn)單配置
FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();
System.out.println(foo.getFoo());
}
}
例子二:List類型
如果配置內(nèi)容是List類型呢?比如:
com.didispace.post[0]=Why Spring Boot com.didispace.post[1]=Why Spring Cloud com.didispace.posts[0].title=Why Spring Boot com.didispace.posts[0].content=It is perfect! com.didispace.posts[1].title=Why Spring Cloud com.didispace.posts[1].content=It is perfect too!
要獲取這些配置依然很簡(jiǎn)單,可以這樣實(shí)現(xiàn):
ApplicationContext context = SpringApplication.run(Application.class, args);
Binder binder = Binder.get(context.getEnvironment());
// 綁定List配置
List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
System.out.println(post);
List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get();
System.out.println(posts);
代碼示例
本文的相關(guān)例子可以查看下面?zhèn)}庫(kù)中的Chapter2-2-1目錄:
Github:https://github.com/dyc87112/SpringBoot-Learning
Gitee:https://gitee.com/didispace/SpringBoot-Learning
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring中@Autowired注解是如何實(shí)現(xiàn)的
在使用java?config的過程當(dāng)中,我們不可避免地會(huì)有各種各樣的注解打交道,其中,我們使用最多的注解應(yīng)該就是@Autowired注解了,這篇文章就來(lái)和大家聊聊它到底怎么實(shí)現(xiàn)的吧2023-07-07
springboot啟動(dòng)后和停止前執(zhí)行方法示例詳解
這篇文章主要介紹了springboot啟動(dòng)后和停止前執(zhí)行方法,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
springboot oauth2實(shí)現(xiàn)單點(diǎn)登錄實(shí)例
我們見過的很多網(wǎng)站,容許使用第三方賬號(hào)登錄,oauth2是用來(lái)做三方登錄的,本文就詳細(xì)的介紹springboot oauth2實(shí)現(xiàn)單點(diǎn)登錄實(shí)例,具有一定的參考價(jià)值,感興趣的可以了解一下2022-01-01
詳解IDEA使用Maven項(xiàng)目不能加入本地Jar包的解決方法
這篇文章主要介紹了詳解IDEA使用Maven項(xiàng)目不能加入本地Jar包的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Jersey Restful接口如何獲取參數(shù)的問題
這篇文章主要介紹了Jersey Restful接口如何獲取參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringMVC使用第三方組件實(shí)現(xiàn)文件上傳
這篇文章主要介紹了SpringMVC使用第三方組件實(shí)現(xiàn)文件上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot中使用Servlet的兩種方式小結(jié)
這篇文章主要介紹了SpringBoot中使用Servlet的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

