I have CDI based application. At runtime my application generates custom event that causes bean reload. Bean reload means go through all beans and reinitialize already injected beans. Reinitialization should be aware of beans dependencies. For example:
class BeanA {
String url;
@PostConstruct
void init(){
url = UrlFactory.getNewUrl()
}
}
class BeanB {
@Inject
BeanA beanA;
@PostConstruct
void init(){
url = beanA.getUrl();
doSomethingWith(url);
}
Hence, when event comes, BeanA and BeanB should be reinitialized in strict order, so during BeanB reinitialization BeanB already aware of new url initialized in BeanA. Is it possible to do at runtime using already existing tools within CDI (something like in Spring: AutowireCapableBeanFactory)? I found that EJB already has @DependsOn annotation that is capable of building bean order during application start up.
The most brute force solution I came up with is listen to one of CDI events during application start up, collect all beans and build dependency graph and during reload go through this graph and do reinitialization. I'm afraid I'm not aware of many pitfalls that I might run into (like cyclic dependencies, lazy initialization (it might not work at all in this case) and many others I'm not aware of, because does not have pretty good understanding of how CDI container works internally)
Are there any other existing techniques that actually solve my problem?