0

You can create ConfigurationProperties-Objects with Spring, in various ways.

One way, is to add the @ConfigurationProperties-Annotation to an @Bean-Declaration like so:

@Bean
@ConfigurationProperties("my.property.group")
public MyProperties myProperties() {
   return new MyProperties();
}

which will create a bean from MyProperties-class and consequently use its setters to fill its members with values from the configuration file.

You can also have the annotation directly on the MyProperties-Object like so:

@ConfigurationProperties("my.property.group")
public class MyProperties {
    @Getter @Setter private String myFirstValue;
    @Getter @Setter private String mySecondValue;
}

creating it by placing @EnableConfigurationProperties(MyProperties.class) to any loaded configuration.

It is also possible to create this class in an immutable way, using @ConstructorBinding

 @ConfigurationProperties("my.property.group")
 @ConstructorBinding
 public class MyProperties {
     @Getter private final String myFirstValue;
     @Getter private final String mySecondValue;
     
     public MyProperties(String myFirstValue, String mySecondValue) {
         this.myFirstValue = myFirstValue;
         this.mySecondValue = mySecondValue;
     }
 }

But how can I create immutable ConfigurationProperties in combination with the first @Bean-method?

I tried something like this:

 @Bean
 @ConfigurationProperties("my.property.group")
 // @ConstructorBinding <---- This is not applicable to methods
 public MyProperties myProperties(String myFirstValue, String mySecondValue) {
     return new MyProperties(myFirstValue, mySecondValue);
 }

which tells me, it could not autowire the parameters, I shall consider declaring some beans of type String

reevesy
  • 3,452
  • 1
  • 26
  • 23
  • Does this answer your question? [Using Spring Boot 2.2.0's @ConstructorBinding for multiple Beans](https://stackoverflow.com/questions/58944506/using-spring-boot-2-2-0s-constructorbinding-for-multiple-beans) – omnichord Jan 14 '21 at 19:31

1 Answers1

0

In the Spring Boot reference documentation for @ConstructorBinding it is said:

To use constructor binding the class must be enabled using @EnableConfigurationProperties or configuration property scanning. You cannot use constructor binding with beans that are created by the regular Spring mechanisms (e.g. @Component beans, beans created via @Bean methods or beans loaded using @Import)

See Spring boot documentation

Perhaps some of the answers in Immutable @ConfigurationProperties will be enough to solve your problem.

reevesy
  • 3,452
  • 1
  • 26
  • 23
fatih
  • 1,285
  • 11
  • 27
  • Thank you, Fatih! So far I got it already, as `@ConstructorBinding` has no `METHOD` in the `@Target`-List. And it would be strange, given the name, also. I'd rather have some `@BeanMethodBinding` or something like that. Any clean way to create immutable `@ConfigurationProperties` via `@Bean`-Methods. – derM - not here for BOT dreams Nov 13 '20 at 10:20
  • @derM Maybe the answers [here](https://stackoverflow.com/questions/26137932/immutable-configurationproperties) will help you a little bit for what you want to do. – fatih Nov 13 '20 at 21:57