Imagine the following simplified DI model:
@ApplicationScoped
public class A {
private B b;
@Inject
public A(B b) {
this.B = b;
}
}
@ApplicationScoped
public class B {
private C c;
@Inject
public B(C c) {
this.C = c;
}
}
@ApplicationScoped
public class C {
@PostConstruct
public void start() {
// processing that should begin on startup
}
}
Suppose I want C#start
to be called on deployment completion. The pattern that is generally suggested online is the one presented here but that solution: 1) adds too much boilerplate, 2) adds a new text file for the extension, 3) resorts to the cheat of using toString
solely to trigger the B
proxy to instantiate the actual B
bean underneath which will in turn trigger the C
proxy etc.
Since CDI 1.1, adding the following method to A
is also a solution:
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
B.toString();
}
This solves the first two problems described above but I still need to call a dummy method on B
so that the instantiation/injection chain is triggered and ends up calling the @PostConstruct
annotated method of C
.
Am I missing a cleaner solution to this problem? Does CDI 2.0 address this?