如何使用JNDI在EJB3中获取新的有状态会话Bean? [英] How to use JNDI to obtain a new Stateful Session Bean, in EJB3?

查看:197
本文介绍了如何使用JNDI在EJB3中获取新的有状态会话Bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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 注释,使用各种猜测,如 @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屋!

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