如何在JBOSS中获取SessionContext [英] How to get SessionContext in JBOSS
问题描述
@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中发生了异常异常。
我真的很生气任何人都可以告诉我怎么了非常感谢!
两个第一个解决方案(现场注入和setter方法注入)看起来很好,应该工作。
我对第三个(查找方法)有疑问,因为您没有显示相应的 @Resource(name =sessionContext)
注释,但如果正确使用,它也应该工作。
第四个选项是查找标准名称 java:comp / EJBContext
@Stateless
public class HelloBean实现com.foo.ejb.HelloRemote {
public void hello(){
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext)ic.lookup(java:comp / EJBContext);
System.out.println(通过标准名称查找EJBContext:+ sctxLookup);
} catch(NamingException ex){
throw new IllegalStateException(ex);
}
}
}
这四种方法都是EJB 3符合,并且应该与任何Java EE 5应用服务器一起工作,如在在EJB 3中获取EJBContext的4种方法。请提供您没有获得的异常的完整堆栈跟踪。
I tried several ways in the session bean, like:
@Resource
private SessionContext ctx;
OR
private SessionContext ctx;
@Resource
private void setSessionContext(SessionContext ctx) {
this.sctx = ctx;
}
OR
InitialContext ic = new InitialContext();
SessionContext ctx = (SessionContext) ic.lookup("java:comp/env/sessionContext");
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!
The two first solutions (field injection and setter method injection) look fine and should work.
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.
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);
}
}
}
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屋!