How do I use JNDI to get a new stateful Bean session, in EJB3?
I am trying to use JNDI to get a new Bean state session in a servlet (as a local variable). My method doGet()
has the following:
Bean bean = (Bean) new InitialContext().lookup("beanName");
I tried to include java:comp/env
, but all my attempts resulted in name exceptions.
I am trying to bind a Bean in annotation @Stateful
using various type guesses @Stateful(name="beanName")
and@Stateful(mappedName="beanName")
a source to share
I needed to use annotation @EJB
for the servlet at the class level like below:
@EJB(name="beanName", beanInterface = Bean.class)
Then the lookup in the service method can happen using the name associated with the annotation @EJB
:
Bean beanInstance = (Bean) new InitialContext().lookup("java:comp/env/beanName");
The Bean class itself doesn't need anything other than a simple annotation @Stateful
.
a source to share