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目录:

相关文章
相关标签/搜索