設定値を受け取るクラスを作成
@ConfigurationPropertiesアノテーションが設定されたクラスを作成します。このクラスの場合、「sample.name」と「sample.hoge」という設定値を保持します。
@Component
@ConfigurationProperties(prefix = "sample")
public class SampleConfiguration {
private String name;
private String hoge;
// setterとgetterは省略
}
メタ情報を生成するためのライブラリをビルドスクリプトに追加
上で作ったConfigurationPropertiesアノテーションが設定されたクラスから、メタ情報(jsonファイル)を生成するためのライブラリをビルドスクリプトに追加します。基本的にこのリンク先通りに設定してあげます。configuration-metadata-annotation-processor
Gradleの場合は、以下の様になります。(必要な箇所のみ)
buildscript {
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/plugins-release' }
}
dependencies {
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
}
}
configure(allprojects) {
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-eclipse'
}
dependencies {
optional "org.springframework.boot:spring-boot-configuration-processor"
}
compileJava.dependsOn(processResources)
IDEの設定でアノテーションプロセッサーを有効にする
IntelliJ IDEAの場合は、設定画面の「Annotation Processors」から有効化します。プロジェクトをビルドする
プロジェクトをビルドすることでメタデータが生成されるようになります。今回の場合は、以下のjsonが出力されます。
{
"groups": [{
"name": "sample",
"type": "com.example.SampleConfiguration",
"sourceType": "com.example.SampleConfiguration"
}],
"properties": [
{
"name": "sample.hoge",
"type": "java.lang.String",
"description": "ほげ",
"sourceType": "com.example.SampleConfiguration"
},
{
"name": "sample.name",
"type": "java.lang.String",
"description": "名前",
"sourceType": "com.example.SampleConfiguration"
}
],
"hints": []
}

