With Spring Boot, can one use multiple @Conditional
annotations on one @Bean
definition? If so, is there a predictable order that they are resolved? In other words, can I specify both @ConditionalOnResource
and @ConditionalOnMissingBean
on the same @Bean
def? Which one would take precedence?
I'm using Spring Boot 1.0.2.RELEASE.
Annotation Type ConditionalIndicates that a component is only eligible for registration when all specified conditions match. A condition is any state that can be determined programmatically before the bean definition is due to be registered (see Condition for details).
To conditionally create a bean, we must first create an implementation of Condition. The Condition interface contains the matches method which returns a boolean value. Here, the AuditEnabledCondition class is checking whether audit. enabled is true using the Environment properties.
In Spring Boot, you can use the @ConditionalOnProperty annotation to enable or disable a particular bean based on the presence of a property. This is very useful if you want to provide optional features to your microservice. And that's it. Your optionalClass bean should resolve to null when you specify mybean.
2 Bean conditions. The @ConditionalOnBean and @ConditionalOnMissingBean annotations allow configurations to be skipped based on the presence or absence of specific beans. You can use the value attribute to specify beans by type, or name to specify beans by name.
I don't think the order is important: any Condition
that doesn't match will cause the bean to be omitted. If they all match, it will be included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With