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 annot
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();
}
});
}
});
I like http://code.google.com/p/mycila/wiki/MycilaGuice. This supports Guice 3, other than http://code.google.com/p/guiceyfruit.