Java & J2EE Page 5 - Introduction to Enterprise JavaBeans |
Remember that the intention of the EJB environment is that you will spend most of your time writing business logic rather than network and database "plumbing." Beyond writing the business logic, the only additional thing the bean writer needs to do is to provide lifecycle "hooks" that allow the container to manage the bean. Each of the different types of EJBs discussed earlier (Session, Entity, and MDB) has a slightly different lifecycle, but the common parts are as follows:
The details associated with each type of bean lifecycle will be discussed as they are covered. For now, all you need to know is that
Listing 4.4 contains the lifecycle methods in the example AgencyBean with most of the implementation code removed for clarity Listing 4.4 Lifecycle Methods on the AgencyBean
As you can see, the example AgencyBean implements the SessionBean interface. This means that it must implement the ejbRemove(), ejbActivate(), ejbPassivate(), and setSessionContext() methods. The context passed in setSessionContext() provides a way for the bean to communicate with the container. It is usual to save the session context object in an instance variable for use by the other bean methods. In an Entity bean the ejbCreate() method takes on the role of constructor in that most of the bean initialization will take place in this method, and corresponds to the create() method defined in the home interface. The ejbCreate() method is not defined in the SessionBean interface, because its signature will vary from one EJB to another (as described tomorrow when Session EJBs are discussed in detail). The Deployment Descriptor The final piece of the EJB jigsaw lies in the provision of configuration information, or meta data, for the EJB. This provides a way of communicating the EJB's requirements and structure to the container. If an EJB is to be successfully deployed, the container will have to be provided with extra information, including
All of this essential information is bundled into a deployment descriptor that accompanies the EJB classes. The deployment descriptor is defined as an XML document, and is discussed in more detail later when examining the packaging of an EJB. In addition to the essential information, the deployment descriptor can also carry other metadata such as:
You will see examples of all of these as you progress through this book.
blog comments powered by Disqus |