作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 Java EE 7 WebSocket 端点中注入(inject)一个 @RequestScoped
CDI bean。
但是我收到错误 WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
。
我做错了什么,为什么不可能?
@Named
@RequestScoped
public class Storage {
}
我在这样的端点中 @Inject
:
@ServerEndpoint("/serverpush")
public class ContratoEndpoint {
@Inject
private Storage storage;
}
我得到以下堆栈跟踪:
org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:689)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:90)
at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:165)
at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
最佳答案
如@John 所述,RequestContext
在 WebSocket 方法中未激活。除了使用 Deltaspike(这是一个不错的选择)之外,您还可以编写自己的拦截器来激活/停用 Weld RequestContext
。
当您使用 Wildfly 时,您可以使用 weld 作为提供的依赖项:
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.2.12.Final</version>
<scope>provided</scope>
</dependency>
然后你可以定义一个InterceptorBinding
@RequestContextOperation
:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface RequestContextOperation
{
}
以及相应的RequestContextInterceptor
,我们在其中激活/停用RequestContext
:
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import org.jboss.weld.context.RequestContext;
import org.jboss.weld.context.unbound.Unbound;
@Interceptor
@RequestContextOperation
public class RequestContextInterceptor {
/** The RequestContext */
@Inject
@Unbound
private RequestContext m_requestContext;
/**
*
* @param p_invocationContext
* @return
* @throws Exception
*/
@AroundInvoke
public Object activateRequestContext(final InvocationContext p_invocationContext) throws Exception {
try {
m_requestContext.activate();
return p_invocationContext.proceed();
} finally {
m_requestContext.invalidate();
m_requestContext.deactivate();
}
}
}
然后您可以在您的类或特定方法上使用 @RequestContextOperation
注释:
@ServerEndpoint("/serverpush")
public class ContratoEndpoint {
@Inject
private Storage storage;
@OnMessage
@RequestContextOperation
public String handleMessage(String message){
// Here the @RequestScoped bean is valid thanks to the @RequestContextOperation InterceptorBinding
storage.yourMethod();
....
}
}
关于java - WebSocket 端点和 CDI 注入(inject) : No active contexts for scope RequestScoped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42440851/
我是一名优秀的程序员,十分优秀!