日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析

發(fā)布時間:2025/3/21 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在Spring Boot 2.0中推出了Relaxed Binding 2.0,對原有的屬性綁定功能做了非常多的改進以幫助我們更容易的在Spring應(yīng)用中加載和讀取配置信息。下面本文就來說說Spring Boot 2.0中對配置的改進。

配置文件綁定

簡單類型

在Spring Boot 2.0中對配置屬性加載的時候會除了像1.x版本時候那樣移除特殊字符外,還會將配置均以全小寫的方式進行匹配和加載。所以,下面的4種配置方式都是等價的:

  • properties格式:
spring.jpa.databaseplatform=mysql spring.jpa.database-platform=mysql spring.jpa.databasePlatform=mysql spring.JPA.database_platform=mysql
  • yaml格式:
spring:jpa:databaseplatform: mysqldatabase-platform: mysqldatabasePlatform: mysqldatabase_platform: mysql

?

Tips:推薦使用全小寫配合-分隔符的方式來配置,比如:spring.jpa.database-platform=mysql

List類型

在properties文件中使用[]來定位列表類型,比如:

spring.my-example.url[0]=http://example.com spring.my-example.url[1]=http://spring.io

也支持使用逗號分割的配置方式,上面與下面的配置是等價的:

spring.my-example.url=http://example.com,http://spring.io

而在yaml文件中使用可以使用如下配置:

spring:my-example:url:- http://example.com- http://spring.io

也支持逗號分割的方式:

spring:my-example:url: http://example.com, http://spring.io

注意:在Spring Boot 2.0中對于List類型的配置必須是連續(xù)的,不然會拋出UnboundConfigurationPropertiesException異常,所以如下配置是不允許的:

foo[0]=a foo[2]=b

在Spring Boot 1.x中上述配置是可以的,foo[1]由于沒有配置,它的值會是null

Map類型

Map類型在properties和yaml中的標準配置方式如下:

  • properties格式:
spring.my-example.foo=bar spring.my-example.hello=world
  • yaml格式:
spring:my-example:foo: barhello: world

注意:如果Map類型的key包含非字母數(shù)字和-的字符,需要用[]括起來,比如:

spring:my-example:'[foo.baz]': bar

環(huán)境屬性綁定

簡單類型

在環(huán)境變量中通過小寫轉(zhuǎn)換與.替換_來映射配置文件中的內(nèi)容,比如:環(huán)境變量SPRING_JPA_DATABASEPLATFORM=mysql的配置會產(chǎn)生與在配置文件中設(shè)置spring.jpa.databaseplatform=mysql一樣的效果。

List類型

由于環(huán)境變量中無法使用[和]符號,所以使用_來替代。任何由下劃線包圍的數(shù)字都會被認為是[]的數(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é)尾的話,最后的下劃線可以省略,比如上面例子中的第一條和第三條等價于下面的配置:

MY_FOO_1 = my.foo[1] MY_FOO_1_2 = my.foo[1][2]

系統(tǒng)屬性綁定

簡單類型

系統(tǒng)屬性與文件配置中的類似,都以移除特殊字符并轉(zhuǎn)化小寫后實現(xiàn)綁定,比如下面的命令行參數(shù)都會實現(xiàn)配置spring.jpa.databaseplatform=mysql的效果:

-Dspring.jpa.database-platform=mysql -Dspring.jpa.databasePlatform=mysql -Dspring.JPA.database_platform=mysql

List類型

系統(tǒng)屬性的綁定也與文件屬性的綁定類似,通過[]來標示,比如:

-D"spring.my-example.url[0]=http://example.com" -D"spring.my-example.url[1]=http://spring.io"

同樣的,他也支持逗號分割的方式,比如:

-Dspring.my-example.url=http://example.com,http://spring.io

屬性的讀取

上文介紹了Spring Boot 2.0中對屬性綁定的內(nèi)容,可以看到對于一個屬性我們可以有多種不同的表達,但是如果我們要在Spring應(yīng)用程序的environment中讀取屬性的時候,每個屬性的唯一名稱符合如下規(guī)則:

  • 通過.分離各個元素
  • 最后一個.將前綴與屬性名稱分開
  • 必須是字母(a-z)和數(shù)字(0-9)
  • 必須是小寫字母
  • 用連字符-來分隔單詞
  • 唯一允許的其他字符是[和],用于List的索引
  • 不能以數(shù)字開頭

所以,如果我們要讀取配置文件中spring.jpa.database-platform的配置,可以這樣寫:

this.environment.containsProperty("spring.jpa.database-platform")

而下面的方式是無法獲取到spring.jpa.database-platform配置內(nèi)容的:

this.environment.containsProperty("spring.jpa.databasePlatform")

注意:使用@Value獲取配置內(nèi)容的時候也需要這樣的特點

全新的綁定API

在Spring Boot 2.0中增加了新的綁定API來幫助我們更容易的獲取配置信息。下面舉個例子來幫助大家更容易的理解:

例子一:簡單類型

假設(shè)在propertes配置中有這樣一個配置:com.didispace.foo=bar

我們?yōu)樗鼊?chuàng)建對應(yīng)的配置類:

@Data @ConfigurationProperties(prefix = "com.didispace") public class FooProperties {private String foo;}

接下來,通過最新的Binder就可以這樣來拿配置信息了:

@SpringBootApplication public class Application {public static void main(String[] args) {ApplicationContext context = SpringApplication.run(Application.class, args);Binder binder = Binder.get(context.getEnvironment());// 綁定簡單配置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 Cloudcom.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!

要獲取這些配置依然很簡單,可以這樣實現(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)}庫中的Chapter2-2-1目錄:

  • Github:https://github.com/dyc87112/SpringBoot-Learning
  • Gitee:https://gitee.com/didispace/SpringBoot-Learning

Spring Booot 2.0 新特性詳解正在連載,點擊看看都有哪些解讀

?本文由?程序猿DD-翟永超?創(chuàng)作,采用?CC BY 3.0 CN協(xié)議?進行許可。 可自由轉(zhuǎn)載、引用,但需署名作者且注明文章出處。如轉(zhuǎn)載至微信公眾號,請在文末添加作者公眾號二維碼。

總結(jié)

以上是生活随笔為你收集整理的Spring Boot 2.0 新特性(一):配置绑定 2.0 全解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。