如何使用JNDI在EJB3中获取新的有状态会话Bean? [英] How to use JNDI to obtain a new Stateful Session Bean, in EJB3?
问题描述
我正在尝试使用JNDI在servlet中获取新的有状态会话Bean(作为局部变量)。我的 doGet()
方法具有以下内容:
I'm trying to use JNDI to obtain a new Stateful Session Bean in a servlet (as a local variable). My doGet()
method has the following:
Bean bean = (Bean) new InitialContext().lookup("beanName");
我试过包含 java:comp / env
但我的所有尝试都导致了命名异常。
I've tried including java:comp/env
but all of my attempts have led to naming exceptions.
我试图在 @Stateful $ c中绑定bean $ c>注释,使用各种猜测,如
@Stateful(name =beanName)
和 @Stateful(mappedName =beanName)
I'm attempting to bind the bean in the @Stateful
annotation, using various guesses like @Stateful(name="beanName")
and @Stateful(mappedName="beanName")
推荐答案
我需要的是使用 @EJB
类级别的servlet上的注释,如下所示:
What I needed was to use the @EJB
annotation on the servlet at the class level, as follows:
@EJB(name="beanName", beanInterface = Bean.class)
然后使用 @EJB
注释:
Bean beanInstance = (Bean) new InitialContext().lookup("java:comp/env/beanName");
Bean类本身不需要任何东西,除了普通的 @Stateful
注释。
There is no need for anything in the Bean class itself, other than the plain @Stateful
annotation.
这篇关于如何使用JNDI在EJB3中获取新的有状态会话Bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!