java - 如何延迟 bean,直到所有 spring 云连接器扫描完成?

标签 java spring spring-boot spring-cloud

在所有 Spring Boot 云自动配置完成后,我需要延迟我的应用程序“服务”bean 处理。

“服务”bean 依赖于 SQL DataSource 和其他 Bean(S3 存储库),这是可选的,可以基于云服务配置或直接配置的属性创建(甚至根本没有创建)。底层的 S3 和 SQL 服务都根据各种外部条件进行了完美的配置。它们在直接、间接属性、云等方面表现良好。

所以现在我有类似...

@Autowired(required=false)
S3Storage s3;

@Autowired
SQLDatabase db;

@Bean
MyService myservice() {
  if (s3 != null) {
    return new SQLWithS3Implementation(db, s3);
  } else {
    return new SQLImplementation(db);
  }
}

我对这个 Bean 做了什么,所以在使用 s3 的 cloud 服务(使用 spring-cloud-connectors)之前它不会被处理还是空的?

  • 我不能要求 s3。它并不总是配置。
  • 我不能使用@Lazy。来自其他服务的引用……至少会很困惑(如果可能的话)。

我需要类似...

@ProcessAfter(CloudAutoConfiguration.class)

但实际上如何在 Spring Boot/Spring Cloud 中做到这一点?

最佳答案

在Spring boot中,你可以使用@ConditionalOnBean 文件:http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.html

   @ConditionalOnBean(CloudAutoConfiguration.class)

关于java - 如何延迟 bean,直到所有 spring 云连接器扫描完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42188871/

相关文章:

java - 从java类中的spring文件访问属性

java - 将 Spring Security 添加为依赖项会导致应用程序表现不同

spring-boot - @JdbcTest 检测带有@SpringBootApplication 注释的类

java - 确定 FlowLayout 中 JComponent 的直接邻居

java - 当PK删除不存在的数据时,如何处理Spring Data JPA删除?

java - Spring Boot 的指标未显示在 Prometheus 中

java - Apache Camel 在使用quartz 调度程序调度 ftp 端点时抛出 java.lang.NullPointerException

java - Acceleo - 与 Java 应用程序的跨模型引用

java - 在 Canvas 上显示 svg

java - "if then else "使用规则引擎