码迷,mamicode.com
首页 > 其他好文 > 详细

@ConditionalOnMissingBean

时间:2018-05-26 12:57:02      阅读:1305      评论:0      收藏:0      [点我收藏+]

标签:mes   created   public   ongl   tor   ring   target   aced   config   

org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean

@Conditional(value={OnBeanCondition.class})
@Target(value={METHOD, TYPE})
@Retention(value=RUNTIME)
@Documented

Conditional that only matches when the specified bean classes and/or names are not already contained in the BeanFactory. 

When placed on a @Bean method, the bean class defaults to the return type of the factory method: 
 @Configuration
 public class MyAutoConfiguration {

     @ConditionalOnMissingBean
     @Bean
     public MyService myService() {
         ...
     }

 }

In the sample above the condition will match if no bean of type MyService is already contained in the BeanFactory. 

The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.
Author:Phillip WebbAndy Wilkinson

 

简单一句话就是,如果指定missing的class比如A,那么生成@Bean这个bean的前提条件就是spring工厂没有这个A,如果没有指定missing的bean,那么A就是它自己,也就是一个工厂不能实例化两次这种bean

@ConditionalOnMissingBean

标签:mes   created   public   ongl   tor   ring   target   aced   config   

原文地址:https://www.cnblogs.com/chuliang/p/9092518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!