Accessing EJB from Spring beans by R4R Team

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"


<property name="jndiName" value="ejb/eJBService"/>

<property name="businessInterface" value=""/>

<property name="resourceRef" value="true" />


<bean id="springController" class="">

<property name="ejbService" ref="eJBService" />


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"


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.

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!