How do you initialize an EJB3 Stateful Session Bean ?

Gerald Nguyen
Gerald Nguyen
2 min read ยท
Previous | Next
On this page

In EJB 3, we can obtain a reference to Stateful Session Bean either either by using @EJB annotation or performing JNDI look-up. Yet these 2 methods do not offer you a way to initialize a Stateful SB as EJB 2.1 Home interface’s createMETHODs do.

StatefulSB bean = home.create (arg0, arg1, arg2);

EJB3 defines @Init annotation as a replacement for EJB 2.1 createMETHOD. But it is intended for bean written to 2.1 client view (that means you will need to define an home interface and annotate the bean class with @RemoteHome or @LocalHome annotations)

So, how do you initialize an EJB3 Stateful Session Bean?

Here’s my solution:

This way the Stateful SB is instantiated according to your requirement.


I found a slightly different solution at http://community.jboss.org/message/524703#524703 by Carlo de Wolf. Repost it here for easy comparison:

If you want you can even create a regular create method which mimicks the home interface:

interface MyRemote {
   MyRemote create(String state);
}
 
class MyBean implement MyRemote {
   MyRemote create(String state) {
      this.state = state;
      return ctx.getBusinessObject(MyRemote.class);
   }
}