Another approach for handling deprecation of the locations property of Spring Boot @ConfigurationProperties
09 Feb 2017
So the time has come: with Spring Boot 1.5 the locations property of the @ConfigurationProperties annotation has been removed. It's time to deal with it so I tried to integrate the proposal made at the end of december in existing projects. It worked quite well for most of them, but it could be improved.
A simpler approach
I tried to simplify the approach, without having the need for an application context and without having to scan the classpath to get all the beans annotated with @ApplicationProperties. In fact I removed the annotation and the class described in the mentioned article, and I took advantage of the Spring's @Configuration annotation. With this approach you just need to declare a class like this:
So now you have a configuration class that is going to be picked up by Spring to actually build the application configuration. Now, it's time to declare the beans to make the configuration pojos injectable into application's beans, and it can be done just by declaring them as usual @Beans. For example, given this pojo:
It can be bound to a Spring bean like this (replace the comment I made in the ApplicationConfiguration class):
That's it! So the final class would look like the following. You can keep adding condiguration beans in this class, so you'll keep everything clean and manageable.
You can find the example project here along with tests and example files.