Is it possible to tell Guice to call some method (i.e. init()) after instantinating an object of given type?
I look for functionality similar to @PostConstruct annotation in EJB 3 (and Spring).
Actually, it is possible.
You need to define a TypeListener
to get the functionality going. Something along the lines of the following in your module definition:
bindListener(Matchers.subclassesOf(MyInitClass.class), new TypeListener() {
@Override
public <I> void hear(final TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
typeEncounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(Object i) {
MyInitClass m = (MyInitClass) i;
m.init();
}
});
}
});