A have simple @Configuration class:
@Data //setters and getters
@ComponentScan
@Configuration
@ConfigurationProperties(prefix = "some.prefix")
public class SomeAutoConfig {
public SomeAutoConfig(Map<String, Pojo> properties) {
this.properties= properties;
}
private final Map<String, Pojo> properties;
@Bean
String string() {
return "test string";
}
}
On debug I have breakpoints on constructor(this.properties= properties), and @Bean method(return "test string").
I don't see properties as constructor parameter, but in @Bean method there are visible.
Why are there see properties only then? Is good way to Iinject this bean better and can be accisible from constructor too?
This is because Spring initializes this field via setter, between constructor execution and the call to the @Bean
annotated method. AFAIK, there's no direct way to do this via constructor, you could use @Value("#{${some.prefix}}")
, but this would require to define properties peculiarly, i.e. {key1:'value1',key2:'value2',....}
EDIT: Since Springboot 2.2, you can leverage @ConstructorBinding
annotation.