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

在Spring Boot 2.0中推出了Relaxed Binding 2.0,對原有的屬性綁定功能作了很是多的改進以幫助咱們更容易的在Spring應用中加載和讀取配置信息。下面本文就來講說Spring Boot 2.0中對配置的改進。
本文首發:http://blog.didispace.com/Spring-Boot-2-0-feature-1-relaxed-binding-2/java

配置文件綁定

簡單類型

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

  • 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:推薦使用全小寫配合-分隔符的方式來配置,好比:spring.jpa.database-platform=mysqlgit

List類型

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

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

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

spring.my-example.url=http://example.com,http://spring.io
複製代碼

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

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類型的配置必須是連續的,否則會拋出UnboundConfigurationPropertiesException異常,因此以下配置是不容許的:bash

foo[0]=a
foo[2]=b
複製代碼

在Spring Boot 1.x中上述配置是能夠的,foo[1]因爲沒有配置,它的值會是nullpost

Map類型

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

  • properties格式:
spring.my-example.foo=bar
spring.my-example.hello=world
複製代碼
  • yaml格式:
spring:
 my-example:
 foo: bar
 hello: world
複製代碼

注意:若是Map類型的key包含非字母數字和-的字符,須要用[]括起來,好比:

spring:
 my-example:
    '[foo.baz]': bar
複製代碼

環境屬性綁定

簡單類型

在環境變量中經過小寫轉換與.替換_來映射配置文件中的內容,好比:環境變量SPRING_JPA_DATABASEPLATFORM=mysql的配置會產生與在配置文件中設置spring.jpa.databaseplatform=mysql同樣的效果。

List類型

因爲環境變量中沒法使用[]符號,因此使用_來替代。任何由下劃線包圍的數字都會被認爲是[]的數組形式。好比:

MY_FOO_1_ = my.foo[1]
MY_FOO_1_BAR = my.foo[1].bar
MY_FOO_1_2_ = my.foo[1][2]
複製代碼

另外,最後環境變量最後是以數字和下劃線結尾的話,最後的下劃線能夠省略,好比上面例子中的第一條和第三條等價於下面的配置:

MY_FOO_1 = my.foo[1]
MY_FOO_1_2 = my.foo[1][2]
複製代碼

系統屬性綁定

簡單類型

系統屬性與文件配置中的相似,都以移除特殊字符並轉化小寫後實現綁定,好比下面的命令行參數都會實現配置spring.jpa.databaseplatform=mysql的效果:

-Dspring.jpa.database-platform=mysql
-Dspring.jpa.databasePlatform=mysql
-Dspring.JPA.database_platform=mysql
複製代碼

List類型

系統屬性的綁定也與文件屬性的綁定相似,經過[]來標示,好比:

-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中對屬性綁定的內容,能夠看到對於一個屬性咱們能夠有多種不一樣的表達,可是若是咱們要在Spring應用程序的environment中讀取屬性的時候,每一個屬性的惟一名稱符合以下規則:

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

因此,若是咱們要讀取配置文件中spring.jpa.database-platform的配置,能夠這樣寫:

this.environment.containsProperty("spring.jpa.database-platform")
複製代碼

而下面的方式是沒法獲取到spring.jpa.database-platform配置內容的:

this.environment.containsProperty("spring.jpa.databasePlatform")
複製代碼

注意:使用@Value獲取配置內容的時候也須要這樣的特色

全新的綁定API

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

例子一:簡單類型

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

咱們爲它建立對應的配置類:

@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類型

若是配置內容是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!
複製代碼

要獲取這些配置依然很簡單,能夠這樣實現:

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);
複製代碼

代碼示例

本文的相關例子能夠查看下面倉庫中的Chapter2-2-1目錄:

相關文章
相關標籤/搜索