Inside Scoop on J2EE : Tips and tricks on J2EE and Oracle Application Server by Debu Panda
Updated: 7/11/2006; 9:45:19 AM.

 

 
 

Tuesday, July 11, 2006

EJB 3.0 is now final and many projects have started using EJB 3.0. The good news is that Spring is building some EJB 3.0 support as part of Pitchfork project that will widen acceptance of EJB 3.0. There may be some projects you want to use both EJB 3.0 Session beans and Spring-managed beans.  For example you may have a Stateless EJB that you want to access in your POJO in your web module.

If you have used EJB 2.1 Session beans from Spring then you remember that you had to use a business interface and Springís proxy classes that helped avoid writing Service locator and repetitive JNDI code.  Unfortunately Spring documentation has not been updated how-to access EJB 3.0 Session beans from Spring. Hence this information in not from the source and result of my trial. I could make this to work and in this blog I will outline the steps that  to access EJB 3.0 Session beans from Spring Applications.

The Stateless EJB 

Let us assume that you have an EJB 3.0 Session bean named EmployeeFacade that is used to create and update employees as in the following code:

@Remote
public interface EmployeeFacade {
Employee addEmployee(String name, double salary);
Employee findEmployeeByEmpNo(Long empNo);
}

@Stateless
public class EmployeeFacadeBean  implements EmployeeFacade {
@PersistenceContext
   private EntityManager em;

      public Employee addEmployee(String empName, double sal) {
         Employee emp = new Employee();
         emp.setName(empName);
         emp.setSal(sal);
         em.persist(emp);
         return emp;
      }

  public Employee findEmployeeByEmpNo(Long empNo){
      return em.find(Employee.class,empNo);
}

}


Remember that EJB 3.0 Session beans are POJOs and interfaces are business interfaces and hence you do not have to add an extra business interface as use of EJB 2.1 with Spring required.

Spring POJOs

You want to use the EmployeeFacade Session bean in your Spring bean and you can use setter injection to inject an instance of the EJB 3.0 Session bean as follows:

public class EmployeeFacadeServiceBean implements EmployeeFacadeService  {

protected EmployeeFacade employeeFacade ;
     
  //setter Injection
  public void setEmployeeFacade(EmployeeFacade employeeFacade) {
     this.employeeFacade = employeeFacade;
  }


   public Employee addEmployee(String empName, Double sal){
       return (Employee) this.employeeFacade.addEmployee(empName, sal);  
   }

    public Employee findEmployeeByEmpNo(Long empNo) {
       return (Employee) this.employeeFacade.findEmployeeByEmpNo(empNo);
    }

}

This Spring bean is accessed from the web module.

Spring Configuration

Now let us see the Spring configuration that does the magic:

1. First EJB 3.0 does not require the proxy classes and related configure that you need for EJB 2.x. This is due to the fact that EJB 3.0 does not require a home interface. You can use the  JndiObjectFactoryBean to wire the EJB in Spring configuration as follows:

<bean id = "employeeFacade"                                  
  class = "org.springframework.jndi.JndiObjectFactoryBean">
    <property name = "jndiName"
              value = "java:comp/env/ejb/EmployeeFacade"/>
</bean>


Note that Iíve used a name in the ENC to make it portable. If you want to use a global JNDI name for your EJB, you can do that too. Also note that there is no difference in using either a remote or local interface for EJB 3.0 Session bean because the interfaces are POJIs and what differs is references defined for the interface used that we will see next.

2. You have to make sure that the EJB is referenced from the web module either using ejb-ref or ejb-local-ref in the web.xml or using @EJB annotation in the class level as follows:


@EJB(name="ejb/EmployeeFacade", beanInterface=oracle.ejb30.EmployeeFacade.class)
public class InsertServlet extends HttpServlet
{

..}


3. You configure the POJO using the EJB (EmployeeFacade) as follows:

<bean id="employeeFacadeService" class="oracle.ejb30.EmployeeFacadeServiceBean">
     <property name = "employeeFacade"  ref = "employeeFacade"/>
  </bean>

Is this simple enough? You can combine the power of Spring and EJB 3.0 without much complexity. In my next blog I will discuss how you can develop a Spring-enabled EJB 3.0 Session bean.


9:40:29 AM    comment []

© Copyright 2006 Debu Panda.

PS: These are my own thoughts and not of my employer ..



Click here to visit the Radio UserLand website.
 


July 2006
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Jun   Aug