我的应用程序设置已使用CDI,一切顺利。现在我正在创建一个从第三方库扩展类的新bean。我试图创建类似下面的例子:
@Named("myNewClass")
@ConversationScoped
public class MyNewClass extends ThirdPartyClass {
@Inject
private ApplicationConfig applicationConfig;
@Override
public void doStuff() {
// In this code, applicationConfig will be null.
}
}
调用doStuff时,applicationConfig始终为null。我添加了一个no args构造函数&用@PostConstruct标记的方法,试着看看发生了什么。然后调用构造函数doStuff方法。由于在构造时调用了doStuff,我此时无法使用@Inject注释。
所以我的问题是如何在此时暂停applicationConfig?
我一直在修补BeanManager(这是我用ApplicationConfig.class作为参数调用的函数):
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
BeanManager beanManager = (BeanManager) envCtx.lookup("BeanManager");
Bean myBean = beanManager.getBeans(clazz).iterator().next();
return beanManager.getReference(myBean, clazz, beanManager.createCreationalContext(myBean));
哪个有效,但它正在创建一个新的ApplicationConfig实例。我希望在ConversationScope上找到我认识的那个。
一点信息:我正在使用Seam 3.0,Weld Servlet 1.1.1,这是在Tomcat 6上运行。
答案 0 :(得分:1)
您可以使用@Inject注释构造函数,然后构造函数的任何参数都将成为BeanManager将解析的注入点。它肯定不是理想的做法,但如果它适合你,那就去吧。