我正在使用 Websphere Application Server 8.0.0.5 ,并且在使用CDI时我发现了以下问题。
- 具有
以外的任何范围@Producer
方法的类不能具有@Dependent
- 任何范围不是
醇>@Dependent
的bean都不能在@Producer
方法的类中注入和使用。
在完成上述任务时,我得到:
[10/8/15 14:50:30:764 GMT+05:30] 00000019 InjectInjecti E CWOWB0102E: A JCDI error has occurred: WebBeans context with scope type annotation @ApplicationScoped does not exist within current thread
[10/8/15 14:50:30:766 GMT+05:30] 00000019 BusinessExcep E CNTR0019E: EJB threw an unexpected (non-declared) exception during invocation of method "doSomething". Exception data: javax.ejb.EJBException: Injection failure; nested exception is: javax.enterprise.context.ContextNotActiveException: WebBeans context with scope type annotation @ApplicationScoped does not exist within current thread
javax.enterprise.context.ContextNotActiveException: WebBeans context with scope type annotation @ApplicationScoped does not exist within current thread
at org.apache.webbeans.container.BeanManagerImpl.getContext(BeanManagerImpl.java:321)
at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.getContextualInstance(NormalScopedBeanInterceptorHandler.java:124)
at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.invoke(NormalScopedBeanInterceptorHandler.java:95)
......
复制问题的示例代码:
public class Producer{
@Inject
@MyQualifier
private Properties properties;
@Produces
public Logger getLogger(InjectionPoint ip){
System.out.println(properties);
System.out.println("Injection point is: "+ip);
return Logger.getLogger(ip.getMember().getDeclaringClass().getName());
}
}
public class AnotherProducer{
@Produces
@MyQualifier
@ApplicationScoped
public Properties getProperties(){
Popperties p = new Properties();
p.put("key","value");
return p;
}
}
我的问题是 - 我是否违反了CDI规范或我的容器出了问题?
答案 0 :(得分:1)
根据CDI规范,类型@ApplicationScoped
的范围应始终可用 - 通常意味着此范围内的对象在应用程序的生命周期内存在。
但似乎您在Websphere中遇到了一个错误,如here中所述。如果您想使用@ApplicationScoped
bean,您有以下选项:
@ApplicationScoped
范围(可能会为应用程序服务器支持的某些内容进行过多编码)@ApplicationScoped
,而是将创建的bean缓存在Singleton bean中(如果它们是先前创建的)