1

Hello I have a "common" project which contains all the implementations of various queues I use.

I use 2 queues in my flow due to legacy.

It has Kafka and rabbit.

My project defines 2 beans and sub-beans:

  • KafkaProducer
  • KafkaConsumer
  • RabbitQueue

All containing some sub-beans to handle failures and stuff.

Now this is my questions: I have one microservice that does not use Kafka. I only want to define the Rabbit queue and its beans, but once I import this common jar dependency, It automatically initiates the Kafka beans.

How can this be done in spring without separating to different libraries.

For example:

@Componenet
public class KafkaProducer {

   @Inject
   private KafkaFailureHandler failureHandler;

}

Regards, Ido

Ido Barash
  • 4,856
  • 11
  • 41
  • 78

1 Answers1

0

Add @Lazy annotation to class

If present and set to true, the @Bean or @Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory.

About lazy @Inject, try adding inside Instance<>:

@Inject
Instance<KafkaFailureHandler> failureHandler;

Or by Provider

@Inject
Provider<KafkaFailureHandler> failureHandler;

For on-demand injection of components / resources inject them as Provider

Ori Marko
  • 56,308
  • 23
  • 131
  • 233