Conditional registration of @ConfigurationProperties bean
See original GitHub issueI have an outstanding SO question that I don’t think is supported as a use case as of 1.2.1. Essentially, I would like to use the functionality provided by @ConditionalOnProperty
along with the type-safe @ConfigurationProperties
. Currently, Spring Boot registers the properties bean regardless of whether any of the properties listed on it exist, so it’s impossible to use @ConditionalOnBean(MyConfigProps.class)
(I just get a bean with all null
fields).
I would like either a way to suppress registration of an “empty” properties bean or a condition that matches only if the bean of the specified @ConfigurationProperties
class exists.
Example:
@ConfigurationProperties(prefix = 'my.service')
class ServiceProps {
String apiKey
}
@Configuration
@ConditionalOnConfigurationProperties(ServiceProps.class)
// or @ConditionalOnBean(ServiceProps.class)
class MyServiceAutoConfig {
@Autowired
ServiceProps serviceProps
@Bean MyService myService() // ...
}
Issue Analytics
- State:
- Created 9 years ago
- Comments:15 (13 by maintainers)
Top Results From Across the Web
Spring Boot bean conditional on @ConfigurationProperties ...
Spring Boot gives us typed configuration objects using the @ConfigurationProperties annotation. One of the advantages is getting property ...
Read more >Conditional Beans with Spring Boot - Reflectoring
It allows to load beans conditionally depending on a certain environment property: @Configuration @ConditionalOnProperty( value="module.
Read more >The Spring @ConditionalOnProperty Annotation - Baeldung
In short, the @ConditionalOnProperty enables bean registration only if an environment property is present and has a specific value. By default, ...
Read more >36. Developing auto-configuration and using conditions - Spring
The @ConditionalOnMissingBean is one common example that is used to allow developers to 'override' auto-configuration if they are not happy with your defaults. ......
Read more >Spring - @Bean conditional registration - LogicBig
The purpose of this annotation is to enable/disable target beans from being registered based on some condition. These conditions are applied ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @izeye. I’ve changed the tag.
Sort of, except that if the properties are present, I don’t understand the purpose of a separate
enable
property; it seems like duplication to me. The use case is the exact same as for@ConditionalOnProperty
, just wanting to make use of the type-safe config object instead.