如何在 JBOSS 中获取 SessionContext [英] How to get SessionContext in JBOSS

查看:19
本文介绍了如何在 JBOSS 中获取 SessionContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在会话 bean 中尝试了几种方法,例如:

I tried several ways in the session bean, like:

@Resource
private SessionContext ctx;

private SessionContext ctx;

@Resource
private void setSessionContext(SessionContext ctx) {
  this.sctx = ctx;
}

InitialContext ic = new InitialContext();
SessionContext ctx = (SessionContext) ic.lookup("java:comp/env/sessionContext");

它们都不起作用,JBOSS 中出现了不同的异常.

None of them worked, differnet exceptions occured in JBOSS.

我真的很生气.任何人都可以告诉我出了什么问题.非常感谢!

I really get mad about it. Anyone could tell me what's wrong. Thanks a lot!

推荐答案

前两种解决方案(字段注入和 setter 方法注入)看起来不错,应该可以.

The two first solutions (field injection and setter method injection) look fine and should work.

我对第三个(查找方法)有疑问,因为您没有显示相应的 @Resource(name="sessionContext") 注释,但它应该可以strong> 如果使用得当,也是如此.

I have a doubt about the third one (the lookup approach) as you didn't show the corresponding @Resource(name="sessionContext") annotation but it should work too if properly used.

第四个选项是查找标准名称 java:comp/EJBContext

A fourth option would be to look up the standard name java:comp/EJBContext

@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
  public void hello() {
    try {
      InitialContext ic = new InitialContext();
      SessionContext sctxLookup = 
          (SessionContext) ic.lookup("java:comp/EJBContext");
      System.out.println("look up EJBContext by standard name: " + sctxLookup);
    } catch (NamingException ex) {
      throw new IllegalStateException(ex);
    }
  }
}

这四种方法都符合 EJB 3 标准,并且绝对适用于任何 Java EE 5 应用服务器,正如 在 EJB 3 中获取 EJBContext 的 4 种方法.如果没有,请提供您获得的异常的完整堆栈跟踪.

These four approaches are all EJB 3 compliant and should definitely work with any Java EE 5 app server as reminded in 4 Ways to Get EJBContext in EJB 3. Please provide the full stack trace of the exception that you get if they don't.

这篇关于如何在 JBOSS 中获取 SessionContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆