Cuneyt
Cuneyt

Reputation: 73

Spring boot @ConditionalOnBean does not detect bean missing, throws exception "No qualifying bean of type"

Have this @Configuration class that requires a bean implementing JmsProperties which is declared in the @ConditionalOnBean

@Configuration
@ConditionalOnBean(JmsProperties.class)
public class JmsConfiguration {

    @Inject
    private JmsProperties properties;
    ...
}

Getting exception:

   Caused by: 
   org.springframework.beans.factory.NoSuchBeanDefinitionException:        
   No qualifying bean of type [com.....JmsProperties] found for dependency: 

Isn't the @ConditionalOnBean supposed to detect this missing bean and not try to inject the dependency in the first place?

Thanks in advance for any pointers

Upvotes: 5

Views: 5674

Answers (2)

Stephane Nicoll
Stephane Nicoll

Reputation: 33151

You've cross-posted on the Spring Boot tracker and the developers responded:

@ConditionalOnBean is evaluated after all configuration classes have been processed, i.e you can't use it to make a whole configuration class conditional on the presence of another bean. You can, however, use it where you have to make all of the configuration's beans conditional on the presence of another bean.

Upvotes: 6

Avis
Avis

Reputation: 2237

You must use @ComponentScan(basePackages="". Without using it your app wont discover & register/instantiate all defined beans and hence you would end up with this type of exception i.e. No bean def found.

Upvotes: 0

Related Questions