Skip to content

Instantly share code, notes, and snippets.

@bitbrain
Created October 18, 2014 17:31
Show Gist options
  • Save bitbrain/43eb50f6b9fe26a37b72 to your computer and use it in GitHub Desktop.
Save bitbrain/43eb50f6b9fe26a37b72 to your computer and use it in GitHub Desktop.
public enum PostConstructModule implements Module, TypeListener {
INSTANCE;
/**
* {@inheritDoc}
*
* @see com.google.inject.Module#configure(com.google.inject.Binder)
*/
@Override
public void configure(final Binder binder) {
binder.bindListener(Matchers.any(), this);
}
/**
* Ruft nach der Injection die Postconstruct Methode(n) auf, wenn sie existieren.
*
* <p>
* {@inheritDoc}
*
* @see com.google.inject.spi.TypeListener#hear(com.google.inject.TypeLiteral, com.google.inject.spi.TypeEncounter)
*/
@Override
public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) {
encounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(final I injectee) {
// alle postconstruct Methoden (nie null) ausführen.
for (final Method postConstructMethod : injectee.getClass().getMethods()) {
try {
if (postConstructMethod.getAnnotation(PostConstruct.class) != null) {
postConstructMethod.invoke(injectee);
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment