在spring boot 2.0中推出了relaxed binding 2.0,对原有的属性 绑定 功能做了非常多的改进以帮助我们更容易的在spring应用中加载和读取 配置 信息。下面本文就来说说spring boot 2.0中对配置的改进。
配置文件绑定
简单类型
在spring boot 2.0中对配置属性加载的时候会除了像1.x版本时候那样移除特殊字符外,还会将配置均以全小写的方式进行匹配和加载。所以,下面的4种配置方式都是等价的:
properties格式:
1 2 3 4 |
spring.jpa.databaseplatform=mysql spring.jpa.database-platform=mysql spring.jpa.databaseplatform=mysql spring.jpa.database_platform=mysql |
yaml格式:
1 2 3 4 5 6 |
spring: jpa: databaseplatform: mysql database-platform: mysql databaseplatform: mysql database_platform: mysql |
tips:推荐使用全小写配合-分隔符的方式来配置,比如:spring.jpa.database-platform=mysql
list类型
在properties文件中使用[]来定位列表类型,比如:
1 2 |
spring.my-example.url[ 0 ]=http: //example测试数据 spring.my-example.url[ 1 ]=http: //spring.io |
也支持使用逗号分割的配置方式,上面与下面的配置是等价的:
1 |
spring.my-example.url=http: //example测试数据,http://spring.io |
而在yaml文件中使用可以使用如下配置:
1 2 3 4 5 |
spring: my-example: url: - http: //example测试数据 - http: //spring.io |
也支持逗号分割的方式:
1 2 3 |
spring: my-example: url: http: //example测试数据, http://spring.io |
注意:在spring boot 2.0中对于list类型的配置必须是连续的,不然会抛出 unboundconfigurationpropertiesexception 异常,所以如下配置是不允许的:
1 2 |
foo[ 0 ]=a foo[ 2 ]=b |
在spring boot 1.x中上述配置是可以的, foo[1] 由于没有配置,它的值会是 null
map类型
map类型在properties和yaml中的标准配置方式如下:
properties格式:
1 2 |
spring.my-example.foo=bar spring.my-example.hello=world |
yaml格式:
1 2 3 4 |
spring: my-example: foo: bar hello: world |
注意:如果map类型的key包含非字母数字和-的字符,需要用[]括起来,比如:
1 2 3 |
spring: my-example: '[foo.baz]' : bar |
环境属性绑定
简单类型
在环境变量中通过小写转换与.替换_来映射配置文件中的内容,比如:环境变量spring_jpa_databaseplatform=mysql的配置会产生与在配置文件中设置spring.jpa.databaseplatform=mysql一样的效果。
list类型
由于环境变量中无法使用[和]符号,所以使用_来替代。任何由下划线包围的数字都会被认为是[]的数组形式。比如:
1 2 3 |
my_foo_1_ = my.foo[ 1 ] my_foo_1_bar = my.foo[ 1 ].bar my_foo_1_2_ = my.foo[ 1 ][ 2 ] |
另外,最后环境变量最后是以数字和下划线结尾的话,最后的下划线可以省略,比如上面例子中的第一条和第三条等价于下面的配置:
1 2 |
my_foo_1 = my.foo[ 1 ] my_foo_1_2 = my.foo[ 1 ][ 2 ] |
系统属性绑定
简单类型
系统属性与文件配置中的类似,都以移除特殊字符并转化小写后实现绑定,比如下面的命令行参数都会实现配置spring.jpa.databaseplatform=mysql的效果:
1 2 3 |
-dspring.jpa.database-platform=mysql -dspring.jpa.databaseplatform=mysql -dspring.jpa.database_platform=mysql |
list类型
系统属性的绑定也与文件属性的绑定类似,通过[]来标示,比如:
1 2 |
-d "spring.my-example.url[0]=http://example测试数据" -d "spring.my-example.url[1]=http://spring.io" |
同样的,他也支持逗号分割的方式,比如:
1 |
-dspring.my-example.url=http: //example测试数据,http://spring.io |
属性的读取
上文介绍了spring boot 2.0中对属性绑定的内容,可以看到对于一个属性我们可以有多种不同的表达,但是如果我们要在spring应用程序的environment中读取属性的时候,每个属性的唯一名称符合如下规则:
通过.分离各个元素 最后一个.将前缀与属性名称分开 必须是字母(a-z)和数字(0-9) 必须是小写字母 用连字符-来分隔单词 唯一允许的其他字符是[和],用于list的索引 不能以数字开头所以,如果我们要读取配置文件中spring.jpa.database-platform的配置,可以这样写:
1 |
this .environment.containsproperty( "spring.jpa.database-platform" ) |
而下面的方式是无法获取到spring.jpa.database-platform配置内容的:
1 |
this .environment.containsproperty( "spring.jpa.databaseplatform" ) |
注意:使用@value获取配置内容的时候也需要这样的特点
全新的绑定api
在spring boot 2.0中增加了新的绑定api来帮助我们更容易的获取配置信息。下面举个例子来帮助大家更容易的理解:
例子一:简单类型
假设在propertes配置中有这样一个配置:com.didispace.foo=bar
我们为它创建对应的配置类:
1 2 3 4 5 6 7 |
@data @configurationproperties (prefix = "com.didispace" ) public class fooproperties {
private string foo;
} |
接下来,通过最新的binder就可以这样来拿配置信息了:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@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类型呢?比如:
1 2 3 4 5 6 7 |
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! |
要获取这些配置依然很简单,可以这样实现:
1 2 3 4 5 6 7 8 9 10 |
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目录:
github: https://github测试数据/dyc87112/springboot-learning
gitee: https://gitee测试数据/didispace/springboot-learning
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:http://blog.didispace测试数据/Spring-Boot-2-0-feature-1-relaxed-binding-2/
查看更多关于SpringBoot2.0新特性之配置绑定全解析的详细内容...