Session Bean for Speed-based progressive learning (IV.)

Source: Internet
Author: User
Tags abstract constructor

Session bean requirements, restrictions, and permissions

In the EJB specification, there are some limitations and requirements for the session bean. These limitations point to what the bean must do, what it cannot do, and some of the methods and interfaces that the bean must implement. There are also some specifications that indicate which processes are allowed to be done, in order to avoid you thinking that these processes are prohibited by other restrictions.

Implement Sessionbean interface

Session Bean must implement Javax.ejb.SessionBean interface

Declare the class to be public, not final or abstract

Remember that the EJB container needs to create a bean instance, so the class must be public and not abstract.

Create a pubic, parameterless constructor

Similarly, because the EJB container must create an instance of the bean. If the constructor is protected or private, the container cannot create an instance.

Do not implement the Finalize method

Although you rarely need to define a finalize method, the EJB specification also explicitly prohibits the definition of a Finalize method in session and entity beans. If your bean needs to do any cleanup operations, it should be done in Ejbremove or Ejbpassivate methods.

Implement the Create method and all remote methods

A session bean must implement all the create methods specified in the home interface and all the methods specified in the remote interface. There are some additional requirements when implementing these methods:

. These methods must be public, not static or final

. The parameter and the returned type must be a valid RMI/IIOP return type. In general, this means that the original type (int, char, double, etc.), serializable object, or remote interface must be the same.

. Method names cannot begin with EJBS (this will confuse the EJB Configuration tool and may bring conflicts)

Optional remote Interface implementation

This feature is also important for a bean to implement a remote interface. In the implementation class, the method signature must be the same as in the Reomote interface, except for the point that the method in the implementation class does not throw a remoteexception. That is, although for each method of a remote interface, there must be a corresponding method in the implementation class, and the following declarations are not required:

public class Shoppingcartimpl implements Sessionbean, ShoppingCart

The reason for implementing a remote interface is that the compiler will tell you when a method is omitted, that is, if a method in the remote interface is not implemented, the compiler will produce an error. Otherwise, you will not know that a method has been omitted until you run a configuration tool or a packaging tool. The later you find the error, the longer it will take to correct it.

The problem that you may encounter when implementing a remote interface is to use the This keyword to transfer objects instead of using the Getejbobject method in the session context. Typically, when you use this instead of getejbobject, the compiler will produce an error because it expects an object to implement the remote interface, but the implementation class does. By implementing the remote interface, you pass the compile, but you run into the error, because it is different from the EJB container expectation, the implementation class is not a proper reference to the remote interface.

Tips

Although implementing a remote interface can point to errors at compile time, this can also lead to errors that will not be discovered until the runtime, making it time-consuming to modify. So you'd better not implement the remote interface, but find some errors in the configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.