1. 程式人生 > >Spring Boot 2.0 新特性(一):配置繫結 2.0 全解析

Spring Boot 2.0 新特性(一):配置繫結 2.0 全解析

在Spring Boot 2.0中推出了Relaxed Binding 2.0,對原有的屬性繫結功能做了非常多的改進以幫助我們更容易的在Spring應用中載入和讀取配置資訊。下面本文就來說說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: mysql
    database-platform: mysql
    databasePlatform: mysql
    database_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型別的配置必須是連續的,不然會丟擲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: 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目錄:

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

 本文由 程式猿DD-翟永超 創作,採用 CC BY 3.0 CN協議 進行許可。 可自由轉載、引用,但需署名作者且註明文章出處。如轉載至微信公眾號,請在文末新增作者公眾號二維碼。