实例化对象后,Guice调用init方法 [英] Guice call init method after instantinating an object
本文介绍了实例化对象后,Guice调用init方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以告诉Guice在之后调用某些方法(即init()) 实例化给定类型的对象?
Is it possible to tell Guice to call some method (i.e. init()) after instantinating an object of given type?
我在EJB 3中寻找类似于@PostConstruct批注的功能.
I look for functionality similar to @PostConstruct annotation in EJB 3.
推荐答案
实际上,这是可能的.
您需要定义一个TypeListener
才能使功能正常运行.在模块定义中遵循以下内容:
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();
}
});
}
});
这篇关于实例化对象后,Guice调用init方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文