@ConditionalOnBean не обнаруживает бин, даже если он создан - PullRequest
0 голосов
/ 28 февраля 2020

У меня есть класс с аннотацией @Configuration и bean-компонентом

  @Configuration
  public class ExampleServiceConfig {

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

  }

У меня есть другой класс обслуживания, который зависит от bean-компонента выше:

@ConditionalOnBean(ExampleServices.class)
public class AnotherService{

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

В журналах отладки весны Я вижу, что первый компонент создается:

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

Но 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)

Почему AnotherService () не создается, даже если Bean-компонент ExampleService был успешно создан?

Кроме того, после создания bean-компонента ExampleService я вижу журнал "не совпадает".

1 Ответ

1 голос
/ 28 февраля 2020

Я думаю, добавление @DependsOn к смеси может решить вашу проблему. Вот еще один ответ, несколько похожий на вашу проблему: { ссылка }

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

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