Accessing EJB from Spring beans
Local SLSBs(Stateless Session Bean)-
Spring Framework especially supports accessing stateless type of EJB. The obtained EJB is here treated like an ordinary Spring bean (type: org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean). There needs to be specified the JNDI name of the EJB and the business interface in Spring bean definition. Additionally, when the resourceRef attribute is set to true then the mandatory JNDI prefix: java:comp/env is added automatically to the name.
<bean id="eJBService"
class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
<property name="jndiName" value="ejb/eJBService"/>
<property name="businessInterface" value="com.r4r.in.eJBService"/>
<property name="resourceRef" value="true" />
</bean>
<bean id="springController" class="com.r4r.in.springController">
<property name="ejbService" ref="eJBService" />
</bean>
The springController bean definition sets the eJBService property
of the controller class to the EJB proxy.
Alternatively (and preferably in case of many such proxy definitions), consider
using the <jee:local-slsb> configuration element in Spring’s "jee"
namespace:
<jee:local-slsb id="eJBService"
business-interface="com.r4r.in.ejbService"
jndi-name="ejb/eJBService" resource-ref="true" />
This EJB access mechanism delivers huge simplification of application code: the web tier code has no dependence on the use of EJB. If we want to replace this EJB reference with a POJO or a mock object or other test stub, we could simply change the eJBService bean definition without changing a line of Java code. Additionally, we haven’t had to write a single line of JNDI lookup or other EJB plumbing code as part of our application.