Spring bean depends on a conditional bean

后端 未结 4 756
小鲜肉
小鲜肉 2021-02-19 02:58

I want a spring bean to be instanciated after another bean. So I simply use the @DependsOn annotation.

The thing is : this other bean is a

4条回答
  •  孤城傲影
    2021-02-19 03:15

    You can use custom condition class:

    public class BeanPresennceCondition implements Condition {
    
      @Override
      public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
          FirstBean firstBean = null;
          try {
          firstBean = (FirstBean)context.getBeanFactory().getBean("firstBean"); 
          }catch(NoSuchBeanDefinitionException ex) {
    
          }
         return firstBean != null;
      }
    }
    

提交回复
热议问题