如何在 Servlet 中注入 ConversationScoped beans

发布于 2024-10-15 06:59:21 字数 407 浏览 2 评论 0原文

我需要将 ConversationScoped bean 注入到 servlet 中。我使用标准的简单 @Inject 标签,并使用 cid 参数调用 servlet,但是当它调用注入 bean 中的任何方法时,我收到以下错误:

org.jboss.weld.context.ContextNotActiveExceptionWELD-001303范围类型没有活动上下文javax.enterprise.context.ConversationScoped

我可以注入吗servlet 中的这些 bean 还是我只能注入 Session 和 Request 范围的 bean?

I need to inject a ConversationScoped bean into a servlet. i use the standard simple @Inject tag and I invoke the servlet with the cid parameter but when it invokes any method in the injected bean I get the following error:

org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped

Can I inject these beans in servlets or I can inject only Session and Request scoped beans?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2024-10-22 06:59:21

在 Servlet 中,上下文是应用程序上下文,这就是您失去对话范围的原因。
这是一个小型实用程序类,如果您希望在 servlet 中支持对话范围,您可以将其用作匿名类并包装请求...

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.jboss.weld.Container;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.context.ConversationContext;
import org.jboss.weld.servlet.ConversationBeanStore;


public abstract class ConversationalHttpRequest {
    protected HttpServletRequest request;


    public ConversationalHttpRequest(HttpServletRequest request) {
        this.request = request;
    }

    public abstract void process() throws Exception;

    public void run() throws ServletException {
        try {
            initConversationContext();
            process();
        } catch (Exception e) {
            throw new ServletException("Error processing conversational request", e);
        } finally {
            cleanupConversationContext();
        }
    }

    private void initConversationContext() {
        ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
        conversationContext.setBeanStore(new ConversationBeanStore(request.getSession(), request.getParameter("cid")));
        conversationContext.setActive(true);
    }

    private void cleanupConversationContext() {
        ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
        conversationContext.setBeanStore(null);
        conversationContext.setActive(false);
    }

}

In a servlet the context is application context that's why you loose conversation scope.
Here is a small utility class that you can use as an anonymous class and wrap the request with if you want conversation scope support in servlets...

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.jboss.weld.Container;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.context.ConversationContext;
import org.jboss.weld.servlet.ConversationBeanStore;


public abstract class ConversationalHttpRequest {
    protected HttpServletRequest request;


    public ConversationalHttpRequest(HttpServletRequest request) {
        this.request = request;
    }

    public abstract void process() throws Exception;

    public void run() throws ServletException {
        try {
            initConversationContext();
            process();
        } catch (Exception e) {
            throw new ServletException("Error processing conversational request", e);
        } finally {
            cleanupConversationContext();
        }
    }

    private void initConversationContext() {
        ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
        conversationContext.setBeanStore(new ConversationBeanStore(request.getSession(), request.getParameter("cid")));
        conversationContext.setActive(true);
    }

    private void cleanupConversationContext() {
        ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
        conversationContext.setBeanStore(null);
        conversationContext.setActive(false);
    }

}
爱你不解释 2024-10-22 06:59:21

如果我们不使用 Weld,那么 Java EE 中之前的答案中提出的 ConversationContext 的等价物是什么?

What is the equivalent of ConversationContext proposed in the previous answer in Java EE if we don't use Weld?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文