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フォーマット:

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-plotform=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_DATABASEPLASTFORM=mysqlの配置は、プロファイルにspring.jpa.databaseplaytform=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.databaseplotform=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-plotformの構成を読み込むなら、こう書くことができます。
    
    this.environment.containsProperty("spring.jpa.database-platform")
    次の方式はspring.jpa.database-plotformの構成内容を取得することができません。
    
    this.environment.containsProperty("spring.jpa.databasePlatform")
    注意:@Valueを使って設定内容を取得する場合もこのような特徴が必要です。
    新しいバインディングAPI
    Spring Boot 2.0に新たなバインディングAPIを追加して、構成情報の入手を容易にしてくれます。以下の例を挙げて、皆さんの分かりやすい理解をお手伝いします。
    例一:シンプルタイプ
    propertes構成には、comp.dispace.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);
    コードの例
    本明細書の関連例は、以下の倉庫のChapter 2-2-1ディレクトリを見ることができる。
    Github:https://github.com/dyc87112/SpringBoot-Learning
    Gite:https://gitee.com/didispace/SpringBoot-Learning
    以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。