WebSocket 端点和 CDI 注入:范围 RequestScoped 没有活动上下文

WebSocket endpoint and CDI injection: No active contexts for scope RequestScoped

我想在我的 Java EE 7 WebSocket 端点中注入一个 @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)

WebSocket 不会为其方法调用初始化请求范围上下文。您可以使用 deltaspike context control 手动启动方法调用的请求上下文。

如@John 所述,RequestContext 在 WebSocket 方法中未激活。除了使用 Deltaspike(这是一个不错的选择)之外,您还可以编写自己的拦截器来 activate/deactivate 焊接 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 其中我们activate/deactivate 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();
        }
    }
}

然后您可以在 class 或特定方法上使用 @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();
        ....
    }

}