Is it possible to have immutable (final) fields with Spring Boot's @ConfigurationProperties
annotation? Example below
@ConfigurationProperties(prefix = "example")
public final class MyProps {
private final String neededProperty;
public MyProps(String neededProperty) {
this.neededProperty = neededProperty;
}
public String getNeededProperty() { .. }
}
Approaches I've tried so far:
- Creating a
@Bean
of the MyProps
class with two constructors
- Providing two constructors: empty and with
neededProperty
argument
- The bean is created with
new MyProps()
- Results in the field being
null
- Using
@ComponentScan
and @Component
to provide the MyProps
bean.
- Results in
BeanInstantiationException
-> NoSuchMethodException: MyProps.<init>()
The only way I have got it working is by providing getter/setter for each non-final field.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…