Skip to content

标记 @ConditionalOnBean 的实例通过 getBeansOfType 时没有成功初始化 #2

@JonasGao

Description

@JonasGao

现有场景如下

class Configuration1 implements ApplicationContextAware {
  @Bean
  @ConditionalOnBean(Interface1.class)
  Service1 service1(Interface1 interface1) {
    return new Service1(interface1);
  }

  @Bean
  Service2 service2() {
    Service1 service1 = applicationContext.getBeansOfType(Service1.class);
    return new Service2(service1);
  }
  
  // TODO about save applicationContext
}

@Configuration
@Import(Configuration1.class)
class AutoConfiguration1 {
}

@Import(Interface1Impl1.class)
@interface EnableInterface1Impl1 {
}

@SpringBootApplication
@ComponentScan("<current pkg>")
@EnableInterface1Impl1
class A {
  public static void main(String[] args) {
    SpringApplication.run(A.class, args);
  }
}

最终启动时,service2 的示例找不到 service1 的示例。也就是这种情况下,@ConditionalOnBean 的判断为 false。最终启动失败

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions