How to use JNDI to obtain a new Stateful Session Bean, in EJB3?

筅森魡賤 提交于 2019-12-05 05:38:04

问题


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");

I've tried including java:comp/env but all of my attempts have led to naming exceptions.

I'm attempting to bind the bean in the @Stateful annotation, using various guesses like @Stateful(name="beanName") and @Stateful(mappedName="beanName")


回答1:


What I needed was to use the @EJB annotation on the servlet at the class level, as follows:

@EJB(name="beanName", beanInterface = Bean.class)

Then lookup in the service method can happen using the name bound by the @EJB annotation:

Bean beanInstance = (Bean) new InitialContext().lookup("java:comp/env/beanName");

There is no need for anything in the Bean class itself, other than the plain @Stateful annotation.



来源:https://stackoverflow.com/questions/2838769/how-to-use-jndi-to-obtain-a-new-stateful-session-bean-in-ejb3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!