Mastering Java EE Development with WildFly
上QQ阅读APP看书,第一时间看更新

Session beans

The EJB specifications define the basic lifecycle and the rules of EJB session beans:

  • A stateless session bean must be part of the @Dependent pseudo-scope
  • A singleton must be part of either the @ApplicationScoped scope or the @Dependent pseudo-scope
  • A stateful session bean can declare any scope

Session beans can be generic types. If the session bean class is a generic type, it is important it has scope @Dependent.

If a session bean is declared as stateful:

  • If it has a @Dependent scope, the application can call the EJB remove method of an instance of the session bean
  • Otherwise, the application cannot directly call the EJB remove method

The main feature of the session beans is passivation. With passivation, a bean is able to temporarily transfer the state of any idle instance to a secondary storage.

We are free to write session beans without EJB but they will use a mock passivation. Here is a session bean:

@SessionScoped
public class SessionBean extends CountBean implements Serializable