EJB3-Session Bean设计原则

 所有会话Bean类型的原则:

The following summarizes the rules that apply to all types of session beans

  • Session Bean必须至少具有一个业务接口
    a session bean must have at least one business interface
  • Session Bean须是具体的。它不能定义为final或abstract
    The session bean class must be concrete. You cannot define a session bean class as either final or abstract since the container needs to manipulate it.
  • Session Bean中须提供无参构造(没有写任何构造方法也可以)
    You must have a no-argument constructor in the bean class. As we saw, this is because the container invokes this constructor to create a bean instance when a client invokes an EJB. Note that the compiler inserts a default noargument constructor if there is no constructor in a Java class.
  • Session Bean可以继承任何POJO(注:还可以继承于其它的Session Bean哦)
    A session bean class can subclass another session bean or any other POJO.For example, a stateless session bean named BidManager can extend another session bean PlaceBidBean in the following way:
    @Stateless
    public BidManagerBean extends PlaceBidBean implements BidManager {
        ...
    }
  • 可以在Session Bean类或父类中定义业务方法和生命周期回调方法
    The business methods and lifecycle callback methods may be defined either in the bean class or in a superclass. It’s worth mentioning here that annotation inheritance is supported with several limitations with EJB 3 session beans. For example, the bean type annotation @Stateless or @Stateful specified in the PlaceBidBean superclass will be ignored when you deploy the BidManagerBean. However, any annotations in the superclasses used to define lifecycle callback methods (more about that later in this section) and resource injections will be inherited by the bean class.
  • 业务方法名称不能以“ejb"开头
    例如,避免使用ejbCreate或ejbAdd等这样的方法名称,因为这可能会干扰EJB的基础结构处理。
    Business method names must not start with “ejb.”
  • 业务方法都必须是public,且不能使用final或static修饰
    注意:如果你想将业务方法定义为远程的,那么要确保方法的参数和返回类型会实现java.io.Serializable接口
    If you are exposing a method in a remote business interface of the EJB, then make sure that the arguments and the return type of the method implement the java.io.Serializable interface.

 

 有状态会话Bean设计原则:

Stateful Session Bean programming rules

  • 有状态会话Bean实例变量必须是Java基本类型(Primary)或者是可序列化的对象类型(Object)
    Stateful bean instance variables used to store conversational state must be Java primitives or Serializable objects.
  • 必须至少定义一个使用@Remove注解的业务方法。
    因为有状态会话Bean不能够像无状态会话Bean一样被池化和重用,所以如果我们没有一个方法去销毁它们时,就会面临它们积累过多的危险。因此我们必须至少定义一个使用@Remove注解的业务方法,让客户端调用该方法来销毁Bean的实例。
    Since stateful session beans cannot be pooled and reused like stateless beans, there is a real danger of accumulating too many of them if we don't have a way to destroy them. Therefore, we have to define a business method for removing the bean instance by the client using the @Remove annotation.
  • 另外,在有状态Bean中除了有@PostConstruct和@PreDestroy标注的生命周期回调方法外,还有@PrePassivate及@PostActivate生命周期回调方法。
    In addition to the PostConstruct and PreDestroy lifecycle callback methods,stateful session beans have the PrePassivate and PostActivate lifecycle callback methods. A PrePassivate method is invoked before a stateful bean instance is passivated and PostActivate is invoked after a bean instance is brought back into the memory and is method ready.

 

 

 

你可能感兴趣的:(EJB3-Session Bean设计原则)