Suppose I create my own custom config properties:
@ConfigurationProperties("a.b")
public record Custom(Topic topics) {
public record Topics(String topicA, String topicB) {}
}
I can supply my properties in the properties file just fine and I can @Autowire
them using the Custom
type just fine.
But if I try to @Autowire
my Topics
it fails.
What is the best way to be able to @Autowire
a nested configuration property type like this?
You can achieve that by declaring a bean for your nested configuration. The bean will receive the base property as a param, and from there you extract the inner record you want:
@Configuration
@EnableConfigurationProperties(Custom.class)
public class TopicsConfiguration {
@Bean
public Topics topics(Custom custom) {
return custom.topics();
}
}