To inject your ejb 3 bean in spring bean you can follow below steps.
1. Create your Spring bean
2. Create your EJB with its Remote and Local Interface
3. Write Implementations class
e.g.
package com.ejb;
@Local
public interface MyEjbLocal{
public String sendMessage();
}
package com.ejb;
@Remote
public interface MyEjbRemote{
public String sendMessage();
}
@Stateless(mappedName = "ejb/MessageSender")
public class MyEjbImpl implements MyEjbLocal, MyEjbRemote{
public String sendMessage(){
return "Hello";
}
}
above is the example of EJB3 which uses both remote and local interface
Now we create the Spring bean in which we inject this ejb.
package com.ejb;
@Service
public class MyService {
private MyEjbLocal ejb;
public void setMyEjbLocal(MyEjbLocal ejb){
this.ejb = ejb;
}
public MyEjbLocal getMyEjbLocal(){
return ejb;
}
}
We have added the instance of ejb in spring however we need to inject this in spring's spring-config.xml.
There are 2 ways to inject the ejb in spring bean
- First Way
<bean id ="myBean" class="org.springframework.ejb.access.LocalStetelessSessionProxyFactoryBean">
<property name="jndiName" value="ejb/MessageSender#com.ejb.MyEjb=Local />
<property name="businessInterface" value="com.ejb.MyEjbLocal" />
</bean>
Note: I have used the Local interface here you can use Remote as per your need.
- Another way of injecting the ejb is
<jee:remote-slsb id="messageSender"
jndi-name="ejb/MessageSender#com.ejb.MyEjbLocal"
business-interface="com.ejb.MyEjbLocal"
home-interface="com.ejb.MyEjbLocal"
cache-home="false" lookup-home-on-startup="false"
refresh-home-on-connect-failure="true" />
So when the bean get initialized at that time the ejb will get injected in your spring bean.