Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you specify multiple @Conditional annotations?

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.

like image 529
Justin Miller Avatar asked Jul 06 '14 14:07

Justin Miller


People also ask

What is @conditional annotation in spring boot?

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).

How do you make conditionally beans?

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.

How do I remove Spring beans based on specific conditions?

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.

Can we have condition based presence or absence of specific bean in spring boot?

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.


1 Answers

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.

like image 109
Dave Syer Avatar answered Oct 04 '22 11:10

Dave Syer