Java spring注解@ConditionalOnMissingBean有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50796810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the spring annotation @ConditionalOnMissingBean do?
提问by Ravi Wadje
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:
我正在尝试启动一个使用此注释的 springboot 应用程序。当我尝试启动应用程序时,它给了我以下错误:
org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException Failed to deduce bean type for com.shutterfly.sbs.platform.SbsPlatformConfigurationClientConfig.getRestTemplate
org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException 无法推断 com.shutterfly.sbs.platform.SbsPlatformConfigurationClientConfig.getRestTemplate 的 bean 类型
Code:
代码:
@ConditionalOnMissingBean
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
回答by Mike Rylander
The @ConditionalOnMissingBean
annotation is a spring conditional annotation for registering beans only when they are not already in the application context.
该@ConditionalOnMissingBean
注解是一个 spring 条件注解,用于仅当 bean 不在应用程序上下文中时才注册它们。
See the documentation: https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/api/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.html
请参阅文档:https: //docs.spring.io/spring-boot/docs/2.0.0.RELEASE/api/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.html
回答by chirag soni
We use @ConditionalOnMissingBean
if we want to include a bean only if a specified bean is not present. For ex.
@ConditionalOnMissingBean
仅当指定的 bean 不存在时,我们才使用if 我们想包含一个 bean。例如。
Let's configure a transactionManager
bean that will only be loaded if a bean of type JpaTransactionManager
is not already defined:
让我们配置一个transactionManager
仅在JpaTransactionManager
尚未定义类型的 bean 时加载的 bean :
@Bean
@ConditionalOnMissingBean(type = "JpaTransactionManager")
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
To understand more consider this scenario as well.
要了解更多,请考虑这种情况。
Let's say in my project I configured a bean videoDecoderService
假设在我的项目中我配置了一个 bean videoDecoderService
@Bean
@ConditionalOnMissingBean(VideoDecoderService.class)
public videoDecoderService videoDecoderService(){
return new VideoDecoderService;
}
What it will do is whoever is using my project would be able to override the videoDecoderService
with the videoDecoderService
of their own. If they are not writing their own videoDecoderService
then this default one will be provided.
它将做的是,无论谁使用我的项目,都可以videoDecoderService
用videoDecoderService
他们自己的覆盖。如果他们不是自己写的,videoDecoderService
那么将提供这个默认的。
回答by Marko Previsic
The @ConditionalOnMissingBeanannotation is used to load a bean only if a given bean is missing:
该@ConditionalOnMissingBean注释用于加载只有在定bean缺少一个bean:
@Bean
@ConditionalOnMissingBean(SomeBean.class)
public SomeBean otherBean(){
return new SomeBean();
}
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 SomeBeanpresent in the application context, the above bean will not be created.
仅当上下文中不存在此类型的其他 bean 时,Spring 才会加载上述 bean。另一方面,如果应用程序上下文中已经存在SomeBean类型的 bean,则不会创建上述 bean。
Some use cases where this annotation comes in handy are:
此注释派上用场的一些用例是:
- Specifying a fallback bean which gets only loaded as a backup if there is no bean of the same type present (for example: using an in-memory database if there is no real database configured)
- Specifying a default bean which allows being overridden in the case that a more specific bean of the same type is present in the context (for example: using a default authentication mechanism unless someone decides to replace it with his own custom authentication)
- 指定一个回退 bean,如果不存在相同类型的 bean,则该 bean 仅作为备份加载(例如:如果没有配置真正的数据库,则使用内存数据库)
- 指定一个默认 bean,允许在上下文中存在相同类型的更具体 bean 的情况下被覆盖(例如:使用默认身份验证机制,除非有人决定用他自己的自定义身份验证替换它)
参考:https: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html