Java
  Home arrow Java arrow Page 3 - Getting Started with Enterprise Java Beans...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Getting Started with Enterprise Java Beans (EJB) 3.0
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2007-01-16

    Table of Contents:
  • Getting Started with Enterprise Java Beans (EJB) 3.0
  • The difference between EJB 2.1 and EJB 3.0
  • Implementing an EJB Step By Step
  • EJB in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More Java Articles
    More By A.P.Rajshekhar


       · There has been a major overhaul of EJB development process between EJB 2.1 and EJB...
       · The article describes clearly about developing the EJB but it should also provide...
     

    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...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek