Search code examples
javanetbeansejbstateless-session-bean

Stateful session beans vs Stateless session beans, Instance variable dilemma


I have a stateless session bean, but I want to add a reference to another bean in order to do some process. So if I add reference to another bean it shows up at instance level variable like this,

@Stateless
public class AccountFacade extends AbstractFacade<Account> implements AccountFacadeRemote         {
    @EJB
    private BillerFacadeRemote billerFacade;

    public AccountFacade() {
    super(Account.class);
    }

    ... all other methods...
}

Question

Now, by definition stateless bean should not have any instance level variables. So I am quite confuse about where to put this private BillerFacadeRemote billerFacade;?


Solution

  • Your code is fine.

    The @EJB annotation is injecting the bean into your class, and the application server manages its lifetime.

    I recommend reading, or skimming the pretty long Java EE tutorial.

    "The EJB container typically creates and maintains a pool of stateless session beans, beginning the stateless session bean’s lifecycle. The container performs any dependency injection and then invokes the method annotated @PostConstruct, if it exists. The bean is now ready to have its business methods invoked by a client."