ging
ging

Reputation: 253

Spring Boot reinject dependency on the fly when using spring cloud binder

I am trying to reinject a specific dependency at runtime and want to update all the places where this bean is injected as dependency. How can i achieve this?

I was reading more about @RefreshScope, but this use case seams a little bit different as i dont want to refresh the whole container which will have a performance hit.

For my usecase, i have a kafka event and based on the header information , i have to update the bean every time the event is received.

I am doing as below:

public classA {

  @Autowired
  Clock clock;
  
  @Autowired 
  ClassB classb;
}

Now on the fly using consumerInterceptor, i am doing as below:

DefaultSingletonbeanRegistry registry = //get Registry;
Clock updatedBean = //new Clock;
registry.destroySingleTon("clock");
registry.createSingleton("clock", updatedBean);

once i create and register a new Bean, how can i update that in classA without refreshing the whole container. Or any other better approaches i can go with here?

Upvotes: 0

Views: 28

Answers (0)

Related Questions