Guice, afterPropertiesSet
Does someone know how I can achieve the same functionality in Guice as the afterPropertiesSet interface in spring? (his construction hook)
source to share
It doesn't seem to be supported yet, so here's a small solution for anyone wanting this work.
public class PostConstructListener implements TypeListener{
private static Logger logger = Logger.getLogger(PostConstructListener.class);
@Override
public <I> void hear(TypeLiteral<I> iTypeLiteral,final TypeEncounter<I> iTypeEncounter) {
Class<? super I> type = iTypeLiteral.getRawType();
ReflectionUtils.MethodFilter mf = new ReflectionUtils.MethodFilter() {
@Override
public boolean matches(Method method) {
return method.isAnnotationPresent(PostConstruct.class);
}
};
ReflectionUtils.MethodCallback mc = new ReflectionUtils.MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
if (!(method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length == 0))
{
logger.warn("Only VOID methods having 0 parameters are supported by the PostConstruct annotation!" +
"method " + method.getName() + " skipped!");
return;
}
iTypeEncounter.register(new PostConstructInvoker<I>(method));
}
};
ReflectionUtils.doWithMethods(type,mc,mf);
}
class PostConstructInvoker<I> implements InjectionListener<I>{
private Method method;
public PostConstructInvoker(Method method) {
this.method = method;
}
@Override
public void afterInjection(I o) {
try {
method.invoke(o);
} catch (Throwable e) {
logger.error(e);
}
}
}
}
The ReflectionUtils package is defined in spring.
Bind this listener to any event with:
bindListener(Matchers.any(),new PostConstructListener());
in your graphics module. Enjoy
source to share
In contrast, the simplest solution, if you are using constructor injection and not doing anything crazy, is to create a postconstruct method and annotate it with @Inject
:
final class FooImpl implements Foo {
private final Bar bar;
@Inject
FooImpl(Bar bar) {
this.bar = bar;
...
}
@Inject
void init() {
// Post-construction code goes here!
}
}
When Guice provides FooImpl, it sees that it has a constructor @Inject
, call it and then search for the methods annotated with @Inject
and call them. The intended use case for this is setter injection, but even if the method @Inject
has no parameters, Guice will call it.
I don't recommend using this if you are using setter or field injection to inject deps as I don't know if Guice gives any guarantees as to the order in which the method is called @Inject
(i.e. yours init()
cannot be guaranteed to be called the last one). However, constructor injection is the preferred approach anyway, so it should be a hassle-free issue.
source to share
I think using @PostConstruct is the way to go.
Here is a related blog post: http://macstrac.blogspot.com/2008/10/adding-support-for-postconstruct.html
And here is the addon library that provides support: http://code.google.com/p/guiceyfruit/
Adding lifecycle support via Guiceyfruit is described here: http://code.google.com/p/guiceyfruit/wiki/Lifecycle
source to share
You need to read the CustomInjections page on the Guice wiki:
In addition to the standard
@Inject
-driven injections , Guice includes custom injection hooks. This allows Guice to host other structures that have their own semantics or injection annotations. Most developers will not use custom injection directly; but they can see their use in extensions and third party libraries. Each custom injection requires a type listener, an injection listener, and registering each one.
source to share