I am trying to start a springboot application where this annotation has been used. When I try to start the application it gives me the following error:
org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException Failed to deduce bean type for com.shutterfly.sbs.platform.SbsPlatformConfigurationClientConfig.getRestTemplate
Code:
@ConditionalOnMissingBean
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
We use
@ConditionalOnMissingBean
if we want to include a bean only if a specified bean is not present. For ex.Let's configure a
transactionManager
bean that will only be loaded if a bean of typeJpaTransactionManager
is not already defined:To understand more consider this scenario as well.
Let's say in my project I configured a bean
videoDecoderService
What it will do is whoever is using my project would be able to override the
videoDecoderService
with thevideoDecoderService
of their own. If they are not writing their ownvideoDecoderService
then this default one will be provided.The @ConditionalOnMissingBean annotation is used to load a bean only if a given bean is missing:
The above bean will get loaded by Spring only if there is no other bean of this type present in the context. On the other hand, if there is already a bean of the type SomeBean present in the application context, the above bean will not be created.
Some use cases where this annotation comes in handy are:
Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html
The
@ConditionalOnMissingBean
annotation is a spring conditional annotation for registering beans only when they are not already in the application context.See the documentation: https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/api/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.html