Guice call init método después de instantinar un objeto
¿Es posible decirle a Guice que llame a algún método (es decir, init ()) después de ¿instantanear un objeto de un tipo dado?
Busco una funcionalidad similar a la anotación @PostConstruct en EJB 3.
5 answers
En Realidad, es posible.
Necesita definir un TypeListener
para que la funcionalidad funcione. Algo parecido a lo siguiente en la definición de tu módulo:
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();
}
});
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-26 18:31:37
Simplemente puede agregar la anotación @Inject
a su método init()
. Se ejecutará automáticamente después de que se instancie el objeto.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-20 14:01:43
Me gusta http://code.google.com/p/mycila/wiki/MycilaGuice . Esto es compatible con la guía 3, que no sea http://code.google.com/p/guiceyfruit .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-15 10:13:10
Guiceyfruit hace lo que buscas para métodos anotados con @PostConstruct
o implementando spring InitializingBean
. También es posible escribir a sus propios oyentes para hacer esto. Este es un ejemplo que llama a un método public init()
después de crear objetos.
import com.google.inject.*;
import com.google.inject.matcher.*;
import com.google.inject.spi.*;
public class MyModule extends AbstractModule {
static class HasInitMethod extends AbstractMatcher<TypeLiteral<?>> {
public boolean matches(TypeLiteral<?> tpe) {
try {
return tpe.getRawType().getMethod("init") != null;
} catch (Exception e) {
return false;
}
}
public static final HasInitMethod INSTANCE = new HasInitMethod();
}
static class InitInvoker implements InjectionListener {
public void afterInjection(Object injectee) {
try {
injectee.getClass().getMethod("init").invoke(injectee);
} catch (Exception e) {
/* do something to handle errors here */
}
}
public static final InitInvoker INSTANCE = new InitInvoker();
}
public void configure() {
bindListener(HasInitMethod.INSTANCE, new TypeListener() {
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
encounter.register(InitInvoker.INSTANCE);
}
});
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-12 13:43:46
GWizard incluye un módulo (gwizard-services
) que proporciona servicios de Guava en un formato amigable con Guice. Los servicios de guayaba le brindan administración del ciclo de vida en subprocesos paralelos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-22 02:22:51