【Spring Boot】@ConfigurationProperties
Spring Bootの@ConfigurationPropertiesをうすくおさらい
@ConfigurationPropertiesを使うとプロパティファイルで定義されたプロパティへ簡単にアクセスすることができる
propertiesファイルを以下のように定義する
user.firstName=John user.lastName=Doe user.age=30
このとき@ConfigurationPropertiesをつけるだけでプロパティが取得できる
@ConfigurationProperties(prefix = "user") public class ConfigProperties { private String firstName; private String lastName; private int age; // standard getters and setters }
@ConfigurationPropertiesがある場合、@Configurationを付けなくても走査してくれる
さらに、プレフィックスを指定するとそのプレフィックスをもつプロパティが取得される
サービスクラスで以下のように呼び出せる
@Service public class AppService { @AutoWired private ConfigProperties properties; public void test() { properties.getFirstName(); } }