Home arrow Java arrow Page 3 - Getting Started with Enterprise Java Beans (EJB) 3.0
JAVA

Getting Started with Enterprise Java Beans (EJB) 3.0


There are significant differences between earlier versions of Enterprise Java Beans (EJB) and EJB 3.0. At least some of these pertain to business components. EJB 3.0 makes it a lot easier to implement the complexity required by many enterprises in fewer steps. Keep reading to learn more.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
January 16, 2007
TABLE OF CONTENTS:
  1. · Getting Started with Enterprise Java Beans (EJB) 3.0
  2. · The difference between EJB 2.1 and EJB 3.0
  3. · Implementing an EJB Step By Step
  4. · EJB in the Real World

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Getting Started with Enterprise Java Beans (EJB) 3.0 - Implementing an EJB Step By Step
(Page 3 of 4 )

The changes in EJB have not only effected the development of EJB but also how the client calls the EJB. Hence the steps to implement EJB can be further divided into implementing the EJB and implementing the client. Both of these make heavy use of annotations instead of XML configurations and/or explicit lookups.

Implementing the EJB

To implement the EJB, though there are no steps such as creating the home interface and remote interface as were necessary with EJB 2.1, the process can still be broken down into certain steps to make implementation process clearer. The steps include specifying the Bean type and developing the POJO. If you contrast these development steps with the steps that need to be followed while developing EJB 2.1 Beans, you can observe that there is no reference to a remote or home interface. With this in mind, let's see the details.

The first step is to specify the Bean type i.e. whether the Bean is Session, Entity or Message Driven. This can be done by using annotations. For specifying a Session Bean use @Stateless and @Stateful, for an Entity Bean it is @Entity and for a Message Driven Bean it is @MessageDriven. Just by using annotations, the requirement of explicitly specifying the interface is being implemented for each type. The type annotation needs to be specified before the class definition.

The next step is developing the POJO. The POJO contains the logic of the Bean. It also contains annotations specifying various configurations, such as the following:

  • The Business interface to be implemented in the case of Session Beans,     both Stateful and Stateless.
  • In the case of Entity Beans, the table with which the POJO is mapped as well as the primary key mappings, named queries etc.
  • The message resources to be mapped in the case of Message Driven Beans.

Let's take the case of a Session Bean as an example. Here is the HelloBean which is a Session Bean:

import javax.ejb.Remote; import javax.ejb.Stateless; /** * Stateless session bean. */ @Stateless @Local(Hello.java) public class HelloBean implements Hello { public String hello() { System.out.println("hello()"); return "Hello, World!"; } }

The annotation @Stateless denotes and tells the server that the type of bean is a Session Bean. The @Local annotation explains the business interface implemented by the Bean. That's the main part. After that it's the Plain Old Java Object which implements business method. There is no requirement for a different home or remote object. The business interface is specific to Session Beans. For other types of EJB there is no business interface. Now let's see the client implementation.

Developing the Client

Any application that requests service from an EJB is an EJB Client. In pre-EJB 3.0 days, to access a service from an EJB, a client would have to first obtain the Bean object using a JNDI name associated with the name. To get this, it had to get the home object from a JNDI lookup and call the create() method. Subsequently, the client called the business methods of the EJB.

However, with EJB 3.0, you do not have to go through those steps. For a client of EJB 3.0 Bean, the reference to the Bean is obtained via dependency injection. By definition a dependency injection is a pattern in which the responsibility for object creation and object linking is removed from the objects themselves and transferred to a factory. In other words the creation and acquiring the reference of an object such as an EJB is delegated to another class implementing factory pattern.

With EJB 3.0, the delegation is done by using annotation. The annotations used for dependency injection are @Inject annotation, the @Resource annotation, and the @EJB annotation. @Resource configures a JNDI value for a field or method;  @EJB is essentially a @Resource where it's known that the result is an EJB interface; and @Inject relies heavily on defaults from the field or method name and type even though it is similar to @Resource. The following example shows how to create a client using the @Inject annotation:

public class HelloClient { public static void main(String[]argv) { @Inject HelloBean; HelloBean helloBean;       System.out.println(helloBean.hello()); } }

@Inject provides the Session Bean object at runtime, taking care of all the steps including the JNDI lookup and obtaining the remote object reference. This simplifies the development process. Now let's see a real world example.


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials