Could you explain me the profit of using @ConfigurationProperties annotation.
I have to options of my code.
First with @ConfigurationProperties annotation:
@Service
@ConfigurationProperties(prefix="myApp")
public class myService() {
private int myProperty;
public vois setMyProperty(int myProperty) {
this.myProperty = myProperty;
}
// And this I can use myProperty which was injected from application.properties (myApp.myProperty = 10)
}
The second one without @ConfigurationProperties annotation.
It looks like this:
@Service
public class myService() {
private final Environment environment;
public myService(Environment environment) {
this.environment = environment;
}
// And this I can use myProperty which was injected from application.properties (myApp.myProperty = 10)
environment.getProperty("myApp.myProperty")
}
With one property the amount of code looks the same, but if I have about 10 properties, the first options will have more boilerplate of code (define 10 properties and 10 setters for this properties).
The second options will have the one injection of Environment and no addition boilerplate code.