[英]@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.