Configファイルで定義した複数の値をListオブジェクトに読み込むサンプルを紹介していきます。
configファイル
sample.list=value1, value2, value3
Config読み込みクラス
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class LoadListSample {
@Bean("loadList")
@ConfigurationProperties(prefix="sample.list")
private List loadList() {
return new ArrayList();
}
@Resource(name = "loadList")
public List configValues;
}
@ConfigurationPropertiesアノテーションで、Bean化時にConfigファイルのsample.list要素をListオブジェクトに読み込んでreturnする処理を定義します。(loadList())
それをloadListというBean名で登録します。
configValuesというフィールドを定義し、ResourceアノテーションでBean処理を経由して値を読み込みます。
これによって、Bean化時にconfgValueにConfigのsample.list要素が読み込まれます。