成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

企業動態
在Spring Boot 2.0中推出了Relaxed Binding 2.0,對原有的屬性綁定功能做了非常多的改進以幫助我們更容易的在Spring應用中加載和讀取配置信息。下面本文就來說說Spring Boot 2.0中對配置的改進。

在Spring Boot 2.0中推出了Relaxed Binding 2.0,對原有的屬性綁定功能做了非常多的改進以幫助我們更容易的在Spring應用中加載和讀取配置信息。下面本文就來說說Spring Boot 2.0中對配置的改進。

配置文件綁定

簡單類型

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

properties格式:

  1. spring.jpa.databaseplatform=mysql 
  2. spring.jpa.database-platform=mysql 
  3. spring.jpa.databasePlatform=mysql 
  4. spring.JPA.database_platform=mysql 

yaml格式:

  1. spring: 
  2.   jpa: 
  3.     databaseplatform: mysql 
  4.     database-platform: mysql 
  5.     databasePlatform: mysql 
  6.     database_platform: mysql 

Tips:推薦使用全小寫配合-分隔符的方式來配置,比如:spring.jpa.database-platform=mysql

List類型

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

  1. spring.my-example.url[0]=http://example.com 
  2. spring.my-example.url[1]=http://spring.io 

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

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

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

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

也支持逗號分割的方式:

  1. spring: 
  2.   my-example: 
  3.     url: http://example.com, http://spring.io 

注意:在Spring Boot 2.0中對于List類型的配置必須是連續的,不然會拋出UnboundConfigurationPropertiesException異常,所以如下配置是不允許的:

  1. foo[0]=a 
  2. foo[2]=b 

在Spring Boot 1.x中上述配置是可以的,foo[1]由于沒有配置,它的值會是null

Map類型

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

  • properties格式:
  1. spring.my-example.foo=bar 
  2. spring.my-example.hello=world 
  • yaml格式:
  1. spring: 
  2.   my-example: 
  3.     foo: bar 
  4.     hello: world 

注意:如果Map類型的key包含非字母數字和-的字符,需要用[]括起來,比如:

  1. spring: 
  2.   my-example: 
  3.     '[foo.baz]': bar 

環境屬性綁定

簡單類型

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

List類型

由于環境變量中無法使用[和]符號,所以使用_來替代。任何由下劃線包圍的數字都會被認為是[]的數組形式。比如:

  1. MY_FOO_1_ = my.foo[1] 
  2. MY_FOO_1_BAR = my.foo[1].bar 
  3. MY_FOO_1_2_ = my.foo[1][2] 

另外,***環境變量***是以數字和下劃線結尾的話,***的下劃線可以省略,比如上面例子中的***條和第三條等價于下面的配置:

  1. MY_FOO_1 = my.foo[1] 
  2. MY_FOO_1_2 = my.foo[1][2] 

系統屬性綁定

簡單類型

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

  1. -Dspring.jpa.database-platform=mysql 
  2. -Dspring.jpa.databasePlatform=mysql 
  3. -Dspring.JPA.database_platform=mysql 

List類型

系統屬性的綁定也與文件屬性的綁定類似,通過[]來標示,比如:

  1. -D"spring.my-example.url[0]=http://example.com" 
  2. -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. @Data 
  2. @ConfigurationProperties(prefix = "com.didispace"
  3. public class FooProperties { 
  4.  
  5.     private String foo; 
  6.  

接下來,通過***的Binder就可以這樣來拿配置信息了:

  1. @SpringBootApplication 
  2. public class Application { 
  3.  
  4.     public static void main(String[] args) { 
  5.         ApplicationContext context = SpringApplication.run(Application.class, args); 
  6.  
  7.         Binder binder = Binder.get(context.getEnvironment()); 
  8.  
  9.         // 綁定簡單配置 
  10.         FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get(); 
  11.         System.out.println(foo.getFoo()); 
  12.     } 

例子二:List類型

如果配置內容是List類型呢?比如:

  1. com.didispace.post[0]=Why Spring Boot 
  2. com.didispace.post[1]=Why Spring Cloud 
  3.  
  4. com.didispace.posts[0].title=Why Spring Boot 
  5. com.didispace.posts[0].content=It is perfect! 
  6. com.didispace.posts[1].title=Why Spring Cloud 
  7. com.didispace.posts[1].content=It is perfect too! 

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

  1. ApplicationContext context = SpringApplication.run(Application.class, args); 
  2.  
  3. Binder binder = Binder.get(context.getEnvironment()); 
  4.  
  5. // 綁定List配置 
  6. List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get(); 
  7. System.out.println(post); 
  8.  
  9. List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get(); 
  10. System.out.println(posts); 

代碼示例

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

Github:https://github.com/dyc87112/SpringBoot-Learning

Gitee:https://gitee.com/didispace/SpringBoot-Learning

【本文為51CTO專欄作者“翟永超”的原創稿件,轉載請通過51CTO聯系作者獲取授權】

戳這里,看該作者更多好文

 

責任編輯:武曉燕 來源: 51CTO專欄
相關推薦

2018-06-06 14:30:38

Spring BootApplication事件

2009-06-15 16:15:37

Spring2.0新特

2009-06-18 15:40:07

Spring Batc

2025-04-16 10:03:40

開發Spring應用程序

2012-03-14 12:29:55

JavaPlay Framwo

2013-02-25 14:02:07

RubyWeb

2025-04-29 07:44:26

配置校驗機制

2009-07-30 14:55:43

ASP.NET 2.0

2010-02-03 17:52:11

Python 2.0

2009-11-04 14:17:34

ADO.NET 2.0

2011-09-30 14:15:10

Sencha ToucSencha Touc

2013-02-26 09:36:57

RubyRuby 2.0

2012-07-02 10:43:49

JVMGroovyJava

2018-06-20 15:33:44

Spring BootJava 9JDK

2021-07-26 11:09:43

NacosSpring Boot配置

2015-06-23 15:48:41

Swift 2.0iOS9

2009-07-03 17:40:35

JSP2.0

2009-08-18 09:17:01

JavaScript2

2021-07-08 18:10:03

2009-06-19 13:28:30

Spring AOPSpring 2.0
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产成人久久精品一区二区三区 | 手机在线观看 | 精品亚洲一区二区 | 国产精品美女久久久久aⅴ国产馆 | 日韩三级 | 国产情侣久久 | 9999国产精品欧美久久久久久 | 国内精品免费久久久久软件老师 | 日本不卡高字幕在线2019 | 成人在线中文字幕 | 91视频在线观看 | 久久亚洲一区 | 午夜影视大全 | 成人av高清在线观看 | 欧美二区在线 | 婷婷久久久久 | 国产精品久久久久久久久久久免费看 | 亚洲伊人久久综合 | 日韩伦理电影免费在线观看 | 国产99久久精品 | 久久亚洲一区二区三 | 午夜男人的天堂 | 国产福利在线小视频 | 国产精品久久久久久久一区探花 | 91av导航 | 国产高清视频一区 | 精品国产91久久久久久 | 丝袜美腿一区 | 91av在线视频观看 | 五月婷六月丁香 | 亚洲导航深夜福利涩涩屋 | 羞羞视频网站在线观看 | 国产在线观看一区二区三区 | 国产乱码久久久久久 | 久久久久久国产精品 | 91精品国产一二三 | 五月婷婷激情网 | 国产又爽又黄的视频 | 看片国产| 亚洲精品久久久久久久不卡四虎 | 亚洲欧美在线观看 |