I got a class (more specifically, a record) MyappProperties that is annotated with @ConfigurationProperties that has a lot of configuration options.
The are several inner classes (more specifically, inner records) in the class that hold configuration options that are specific to a certain functionality of my application.
One of the configuration options, writerImplementation, within one of the inner classes has a default value annotated with @DefaultValue:
@ConfigurationProperties(prefix = "myapp", ignoreUnknownFields = false)
public record MyappProperties(
...
@DefaultValue ConfigForJob configForJob
) {
...
public record ConfigForJob (
@DefaultValue("MongoTemplate") String writerImplementation
) {
...
}
}
I want to use the value of the configuration option writerImplementation to conditionally make a Spring Bean available within the application context depending on the value of the configuration option:
@Bean
@Autowired
@ConditionalOnProperty(name = "myapp.config-for-job.writer-implementation", havingValue = "MongoTemplate")
public Tasklet mongoTemplateTasklet(MongoTemplate mongoTemplate) {
...
}
@Bean
@Autowired
@ConditionalOnProperty(name = "myapp.config-for-job.writer-implementation", havingValue = "MongoRepository")
public Tasklet mongoRepositoryTasklet(MongoRepository mongoRepository) {
...
}
This, however, does not to work. The application fails to start with the message that there isn't a Bean mongoTemplateTasklet available. If I manually set the value for the property with the key myapp.config-for-job.writer-implementation (that is, by putting it in application.properties or command line with -Dmyapp.config-for-job.writer-implementation=MongoTemplate) it works fine.
So it seems like the default value set via the @DefaultValue annotation is not taken up by Spring (or not taken up in time to be available for evaluating the @ConditionalOnProperty annotation).
Any help is much appreciated :)