Showing posts with label EJB. Show all posts
Showing posts with label EJB. Show all posts

March 30, 2009

Spring integration with EJB 2.x Tutorial (Part 1)

In my last post on this topic I have explained two places where Spring can be integrate with EJB 2.x. In the first part of this tutorial I am giving step by step example for creating Stateless EJB using Spring. Here I am developing this application in RAD 7.
  1. Create a new Enterprise Application Project as ‘SpringTutorial’. Add two new modules to it:
    a) Application Client module named as ‘SpringTutorialClient’.

    b) EJB module named as ‘SpringTutorialEJB’.

  2. First we will work on EJB module and try to create an Stateless bean as Spring resource. In SpringTutorialEJB stateless EJB will have 3 classes as usual. Named as SpringEJB,SpringHome and SpringRemote.

  3. Service logic can be associated with bean using interface. In our case,it will be UserNameService interface and implementation will be in UserNameServiceImpl class.

  4. Creating UserNameService interface:

    package com.company.service;

    public interface UserNameService {

    public boolean checkName (String name);
    }

  5. Creating UserNameServiceImpl class:

    package com.company.service;

    public class UserNameServiceImpl implements UserNameService{

    public boolean checkName(String name) {
    if (name.equals("admin"))
    return true;
    else
    return false;
    }

    }

  6. Next step is to create Remote interface SpringRemote for our SpringEJB:

    package com.mycompany.ejb;

    import javax.ejb.EJBObject;

    import com.company.service.UserNameService;

    public interface SpringRemote extends UserNameService , EJBObject{

    }

    Here no business method is defined as we are extending UserNameService interface to it.

  7. Home Interface will look like:

    package com.mycompany.ejb;

    import javax.ejb.CreateException;
    import javax.ejb.EJBHome;

    public interface SpringHome extends EJBHome{

    public SpringRemote create() throws CreateException,java.rmi.RemoteException;

    }

  8. Now comes the bean class:

    package com.mycompany.ejb;

    import javax.ejb.CreateException;

    import org.springframework.ejb.support.AbstractStatelessSessionBean;

    import com.company.service.UserNameService;

    public class SpringEJB extends AbstractStatelessSessionBean implements UserNameService{

    public static final String BEAN_NAME = "userNameService";

    private UserNameService service;

    @Override
    protected void onEjbCreate() throws CreateException {

    service = (UserNameService)getBeanFactory().getBean(BEAN_NAME);

    }

    public boolean checkName(String name) {

    return service.checkName(name);
    }

    }

    Few points to be noted here:

    a) Bean class SpringEJB extends the Spring Base class AbstractStatelessSessionBean and implements the UserNameService.

    b) This has no ejbXXX () methods as these are all implemented in AbstractStatelessSessionBean.

    c) onEjbCreate() method is called during ejbCreate(). It is used to obtain any Spring-managed resources the EJB bean needs.

  9. Bean configuration in EJB Deployment Descriptor:

    <session>
    <description>User Name Service </description>
    <ejb-name>UserNameService</ejb-name>
    <home>com.mycompany.ejb.SpringHome</home>
    <remote>com.mycompany.ejb.SpringRemote</remote>
    <ejb-class>com.mycompany.ejb.SpringEJB</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Container</transaction-type>
    <env-entry>
    <env-entry-name>ejb/BeanFactoryPath</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>]
    <env-entry-value>applicationContext.xml</env-entry-value>
    </env-entry>
    </session>

    The important part in DD is the <env-entry> tag that sets the value of ejb/BeanFactoryPath JNDI location to applicationContext.xml.

  10. applicationContext will look like:

    <beans>
    <bean id="userNameService" class="com.mycompany.service.UserNameServiceImpl"/>
    </beans>

  11. You can deploy this ejb on desired server and test it using JNDI lookup.
In next post I will try to explain accessing stateless EJBs with the help of Spring.
Till then Happy Coding!!!

    March 8, 2009

    Spring integration with EJB 2.x

    These days I am working on a bank web application. This application needs to be developed in synch with an 8 years old web application. The problem is as old application is running fine from a long time; manager wants us to develop it using the same frameworks. The ancient configuration is struts1.3 + EJB 2.1 + iBatis which is not it all test friendly.

    Finally we are trying our luck with the kinda fusion of both old and new.  For the middle layer I am trying to minimize side effects of EJB 2.1 by wrapping it around with Spring.

    Spring's support to EJB integration can be grouped into two categories:

    1. EJB access using Spring
    2. EJB implementaion using Spring

    Accessing EJB with the help of Spring, makes it easy. Spring extends basic JNDI support framework and utilizes AOP support to provide proxy-based access to EJB resources.

    Spring provides abstract base classes to support EJB implementation. Abstract classes help to create 3 types of EJBs.

    • Stateless Session Beans
    • Stateful Session Beans
    • Message Driven Beans

    There is no support to Entity beans. 
    Spring implementation not only eases the burden of creating EJB but makes easy access to Spring managed resources within the beans. It helps to factor business logic out of the EJB implementation and into a POJO used by EJB.

    Till now I have been able to implement EJB with Spring and its successfully deployed in WebSphere Application Server  5.1.

    May be next time I will post a tutorial on this exercise.