Wednesday, March 14, 2012

EJB3 Dependency Injection




As we discussed in previous post about creation of stateless session bean, context lookup to get the bean in client code and invoking the EJB method.

In this post, We will examine the dependency injection capability of  EJB3.0 and we will use annotation to inject the session bean into our client code.



Assuming that there's only one EJB which implements the HelloWorldIx business interface in our application, we can inject it using an empty @EJB annotation:
      @EJB
      private HelloWorldIx helloWorldIx;

EJB is injected (auto wiring) into client by the application server. The bean name is resolved automatically if there's only one EJB implementing the requested business interface in the application.

If a business interface is implemented by more than one EJB in application, then use beanName element of @EJB to inject the appropriate bean as :

      @EJB(beanName="HelloWorldSessionBean")
      private HelloWorldIx helloWorldIx;


The beanName element specifies the bean "name" as declared in the @Stateful or @Stateless annotations via the name element or in the deployment descriptor via the <ejbname/> element like :           
      @Stateless(name="HelloWorldSessionBean").

This beanName element is useful when more than one EJB implement the same business interface in an application. the beanName allows  developer to reference/inject a specific EJB.

We have a simple business interface called HelloWorldIx which is implemented by session bean namely HelloWorldSessionBean as shown in figure 1.



We will write a servlet to inject session bean in Java EE web module as metioned below: 


(1)   Client will submit a post request to servlet.
(2)   Post method use injected reference to invoke EJB method.

N.B. The container creates servlet instance and all the dependencies on resources, beans are injected into the servlet before any method call.

  

HelloWorldIx.java
package com.sarf.ejb3.bean;
import javax.ejb.Local;
@Local
public interface HelloWorldIx {
      public String sayHello();
}



 
HelloWorldSessionBean.java
package com.sarf.ejb3.bean;
import javax.ejb.Local;
import javax.ejb.Stateless;

@Stateless(name="HelloWorldSessionBean")
@Local(HelloWorldIx.class)
public class HelloWorldSessionBean implements HelloWorldIx{

      @Override
      public String sayHello() {
            return "Hello, EJB3 ! Message from stateless session bean";
      }
}




 
InjectController.java

package com.sarf.ejb3.controller;

import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sarf.ejb3.bean.HelloWorldIx;

public class InjectController extends HttpServlet {
      private static final long serialVersionUID = 1L;
      //Auto Wiring 
      @EJB
      private HelloWorldIx helloWorldIx;
          
      protected void doGet(HttpServletRequest request, 
        HttpServletResponse response) throws ServletException, IOException {
            processRequest(request,response);
      }

      protected void doPost(HttpServletRequest request, 
        HttpServletResponse response) throws ServletException, IOException {
            processRequest(request,response);
      }
      
      public void processRequest(HttpServletRequest request, 
        HttpServletResponse response) throws ServletException, IOException {
            String message = "";
            if(helloWorldIx != null)
             message = helloWorldIx.sayHello();
           
            request.setAttribute("msg", message);
            RequestDispatcher rd = 
             request.getRequestDispatcher("./jsp/output.jsp");
            rd.forward(request, response);
      }
}


 
web.xml




N.B. Servlet API version 2.5 or above supports java annotation which requires JavaSE5. Check the web.xml version which is version="2.5". For more info : Sun Site


We will use index.jsp to submit  request to servlet and the servlet will forward the response to output.jsp page.

index.jsp




 
output.jsp





 
application.xml





Setup and Deploy

Resources required
Server : Jboss 5.1.0 GA
Java    : Jdk1.5 or higher
Build   : ANT tool / any

Now we have the package the above mentioned files after compilation into an ear file.