Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delay beans until all spring cloud connectors scan is finished?

I need to delay my application "service" bean processing after all Spring Boot cloud auto-configuration is finished.

"service" bean has dependency both on SQL DataSource and other Bean (S3 repository) which is optional and can be created based on cloud services configuration or directly configured properties (or even not created at all). Both underlying S3 and SQL services are configured perfectly based on various external conditions. They behave well with direct, inderect properties, cloud and so on.

So now I have something like...

@Autowired(required=false)
S3Storage s3;

@Autowired
SQLDatabase db;

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

What have I do with this Bean so it is not processed before cloud services (spring-cloud-connectors is used) with s3 yet null?

  • I cannot make s3 required. It is not always configured.
  • I cannot use @Lazy. References from other services... will be messy at least (if possible).

I need something like...

@ProcessAfter(CloudAutoConfiguration.class)

But how actually to do so in Spring Boot / Spring Cloud?

like image 539
Roman Nikitchenko Avatar asked Dec 20 '25 11:12

Roman Nikitchenko


1 Answers

In Spring boot, you can make use of @ConditionalOnBean Doc : http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnBean.html

   @ConditionalOnBean(CloudAutoConfiguration.class)
like image 126
Barath Avatar answered Dec 23 '25 01:12

Barath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!