激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot2.0新特性之配置綁定全解析

SpringBoot2.0新特性之配置綁定全解析

2021-07-22 16:17翟永超 Java教程

在Spring Boot 2.0中推出了Relaxed Binding 2.0,對原有的屬性綁定功能做了非常多的改進以幫助我們更容易的在Spring應用中加載和讀取配置信息,感興趣的小伙伴們可以參考一下

在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.com
spring.my-example.url[1]=http://spring.io

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

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

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

?
1
2
3
4
5
spring:
 my-example:
  url:
   - http://example.com
   - http://spring.io

也支持逗號分割的方式:

?
1
2
3
spring:
 my-example:
  url: http://example.com, 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.com"
-d"spring.my-example.url[1]=http://spring.io"

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

?
1
-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的配置,可以這樣寫:

?
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.com/dyc87112/springboot-learning
gitee:https://gitee.com/didispace/springboot-learning

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.didispace.com/Spring-Boot-2-0-feature-1-relaxed-binding-2/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人免费在线视频 | 久久精品23 | 成人9禁啪啪无遮挡免费 | 免费看成人毛片 | 日韩一级片免费 | 欧美a区 | 国产精品美女一区二区 | 精品久久一区二区 | 毛片在线免费视频 | 国产精品久久久久久模特 | 久久综合艹 | 911精品影院在线观看 | 97视频一二区| 国产伦精品一区二区三区 | 日韩专区在线 | 成人nv在线观看 | hd porn 4k video xhicial | 蜜桃91丨九色丨蝌蚪91桃色 | 亚洲小视频在线 | 久久草在线视频 | 成人在线观看网 | 久久免费视频8 | av在线免费观看中文字幕 | h视频免费在线观看 | 精品一区二区三区网站 | 国产精品99久久久久久久女警 | 一级毛片免费观看在线 | 一级免费大片 | 欧美亚洲国产一区二区三区 | 国产一区精品在线观看 | 日韩视频一区二区在线观看 | av噜噜噜噜 | 成熟女人特级毛片www免费 | 视频一区二区三区中文字幕 | 欧洲成人一区二区 | 日韩黄色片免费看 | 欧美一级爱爱 | 国产一区二区视频在线播放 | va免费视频 | 国产精品久久久久久久久粉嫩 | 黄色av片在线观看 |