简体   繁体   English

@ConditionalOnBean 即使创建了 bean 也没有检测到它

[英]@ConditionalOnBean not detecting bean even though it is created

I have a class annotated with @Configuration and a bean我有一个用@Configuration 和 bean 注释的类

  @Configuration
  public class ExampleServiceConfig {

  @Bean
  @ConditionalOnProperty(value = servicefeature1.enable", havingValue = "true")
  public ExampleServices exampleServices() {
    return new ExampleServices();

  }

I then have another service class that depends on the bean above:然后我有另一个依赖于上面 bean 的服务类:

@ConditionalOnBean(ExampleServices.class)
public class AnotherService{

   @Autowired
   public AnotherService(ExampleServices exampleServices){
      this.exampleServices = exampleServices
   }
}

In the spring debug logs, I see the first bean is getting created:在 spring 调试日志中,我看到第一个 bean 正在创建:

2020-02-28 14:08:51.841 DEBUG 18158 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'exampleServices'

But the AnotherService() is not getting created:但是 AnotherService() 没有被创建:

AnotherService:
      Did not match:
         - @ConditionalOnBean (types: x.x.x.ExampleServices; SearchStrategy: all) did not find any beans of type x.x.x.ExampleServices (OnBeanCondition)

Why is the AnotherService() not getting created even though the ExampleService bean was created successfully?为什么即使 ExampleService bean 已成功创建,AnotherService() 也没有被创建?

Also, I see the "did not match" log after the ExampleService bean got created.此外,在创建 ExampleService bean 后,我看到了“不匹配”日志。

I think adding @DependsOn to the mix could fix your issue.我认为将@DependsOn添加到组合中可以解决您的问题。 Here is another answer, somewhat similar with your problem: https://stackoverflow.com/a/50518946/6908551这是另一个答案,与您的问题有些相似: https : //stackoverflow.com/a/50518946/6908551

@Component
@ConditionalOnBean(ExampleServices.class)
@DependsOn("exampleServices")
public class AnotherService {

   @Autowired
   public AnotherService(ExampleServices exampleServices) {
      this.exampleServices = exampleServices
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使 Bean 是通过 @Bean 创建的,也会出现 NoSuchBeanDefinitionException - NoSuchBeanDefinitionException coming even though Bean is created via @Bean @ConditionalOnBean 不适用于手动注册的 bean - @ConditionalOnBean does not work with manually registered bean 通过Java实时检测新创建的文件 - Detecting newly created files though Java in realtime 即使依赖 Bean 存在,创建 Bean 时出错 - Error while creating Bean even though the Dependency Bean is there 为什么@SpringBootApplication类中声明的bean即使不是构造型类也要注册? - Why is a bean declared in @SpringBootApplication class registered even though it is not a stereotyped class? 即使我已经正确创建了数据库,“未知数据库” - “unknow database” even though i have created database correctly 即使创建了连接和会话,也没有数据发送给消费者 - No data being sent to consumers, even though connection and session are created 即使受管bean包含方法,服务器抛出消息也没有属性'getCustomerDetails' - Even though the Managed bean contains method, server throwing message as does not have the property 'getCustomerDetails' 即使bean定义写在dispatcher-servlet.xml中,Spring也会自动装配NullpointerException - NullpointerException in Spring autowiring even though bean definition is written in dispatcher-servlet.xml Visual Studio Code 未检测到 java 17 已安装,即使 java 17 是通过从 vsc 获得的链接安装的 - Visual Studio Code not detecting java 17 installed even though java 17 was installed via a link gotten from vsc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM