Created
October 18, 2014 17:31
-
-
Save bitbrain/43eb50f6b9fe26a37b72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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