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:


    public interface UserNameService {

    public boolean checkName (String name);

  5. Creating UserNameServiceImpl class:


    public class UserNameServiceImpl implements UserNameService{

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


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

    package com.mycompany.ejb;

    import javax.ejb.EJBObject;


    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;



    public class SpringEJB extends AbstractStatelessSessionBean implements UserNameService{

    public static final String BEAN_NAME = "userNameService";

    private UserNameService service;

    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:

    <description>User Name Service </description>

    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:

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

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

    1 comment:

    1. Awesome blog! Is your theme custom made or did you download it from somewhere?
      A design like yours with a few simple tweeks would really make my blog jump out.
      Please let me know where you got your design. Many thanks

      Here is my blog post: costume de baie (