1) Introduction
Enterprise Java Beans (EJB) can be used extensively in Spring's environment. In this article, we will know about the transparent support available in Spring for using the Stateless and the Stateful Session Beans in Spring.The pre-requisite for this article is some basic knowledge in Spring which can be got by reading the article in javabeat Introduction to Spring Web Framework.
2) API Support for EJB in Spring
Let us get into the supporting classes/interfaces available in the Spring Distribution for providing integration support of Enterprise Beans. Let us look at the important classes available in the following packages,
- org.springframework.ejb.support
- org.springframework.ejb.access
2.1) Supporting Base classes
This package contains the base supporting classes for creating an enterprise bean. For example, in order to create stateless session bean we can make use of the class 'AbstractStatelessSessionBean'. Similarly, the classes AbstractStatefulSessionBean and AbstractJmsMessageDrivenBean correspond to Stateful Session Bean and Message Driven Bean respectively.
These classes generally form the Facade for the clients. It means that the core logic for doing an operation will be implementated as a POJO (Plain Old Java Object) and the Enterprise Beans merely serve as a facade and delegate the control to POJOs. One of the major disadvantages that the developers would have felt while using the Enterprise Bean running in a EJB Container is that it will be harder for them to perform unit testing. With the use of Spring's approach, unit testing becomes much simplier since the implementation classes are POJO.
The common super-class for all Enterprise Beans is the AbstractEnterpriseBean which contains a well-defined strategy for loading beans from the Application context. The default strategy is to use the ContextJndiBeanFactoryLocator that will create a BeanFactory object with the information taken from one or more classpath locations as specified by the default environment variable,
Consider the following default Bean Factory locating strategy,
<session>
...
<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>
We can see from the above code snippet that the default environment variable corresponds to the default bean factory locator key and in our case it happens to be 'ejb/BeanFactoryPath'. This always can be overridden programmatically by calling the method as follows,
String beanFactoryLocatorKey = "applicationSpecificBeanFactoryLocator";
bean.setBeanFactoryLocatorKey(beanFactoryLocatorKey);
The default BeanFactory Locator is the ContextJndiBeanFactoryLocator and this can also be overridden programmatically by calling the following method,
BeanFactoryLocator myBeanFactoryLocator = ...;
Bean.setBeanFactoryLocator(myBeanFactoryLocator);
2.2) Accessing Enterprise Beans
The mode of accessing a Stateful Session Bean is different from the way a Stateless Session Bean is accessed. Let us look into the support for accessing a Stateless Session Bean (both local and remote).
For accessing a Local Stateless Session Bean, we need to use the supporting class LocalStatelessSessionProxyFactoryBean. The configuration for accessing a local stateless session bean is as follows,
<bean id="myBean"
class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
<property name="jndiName">
<value>ejb/myBeanService</value>
</property>
<property name="businessInterface">
<value>MyComponentService</value>
</property>
</bean>
There are two properties for configuring a local stateless session bean using Proxy mechanism. The first property 'jndiName' is the Jndi name of the bean which can be used by the Jndi Bean Factory object to perform lookup operation. The second property 'businessInterface' defines the business interface for which the implementation will be generated by the Spring container and delegation will be made to the actual implementation.
Similarly, for accessing the Remote Stateless Session Bean, the class SimpleRemoteStatelessSessionProxyFactoryBean has to be used instead of LocalStatelessSessionProxyFactoryBean. The properties 'jndiName' and 'businessInterface' will still be applicable.
There is no direct support in Spring for accessing the Stateful Session Beans using Proxy mechanisms. However, a convenient way is provided in the form of JndiObjectFactoryBean.
Let us consider the following configuration,
<bean id="myServiceHome"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>ejb/myService</value>
</property>
</bean>
In the above Xml snippet, we have configured an instance of a EJB Home interface with the help of JndiObjectFactoryBean. In a traditional EJB Applicaition, we have to manually do a JNDI lookup using the InitialContext to get a reference to the EJB Home object. But in Spring, the lookup operation is made easy merely by putting some configuration information,
We would have to use the following approach to get a reference to the Stateful Session Bean,
ApplicationContext context = .... ; // Initialize the Application context.
MyServiceHome myServiceHome = (MyServiceHome)context.getBean("myServiceHome");
MyService myService = myServiceHome.create();
// Perform the required business logic using this bean.
.
.
.
|