EJB 3 injection into spring beans
Asked Answered
B

2

6

I've made a mavenized web application with spring, spring security... Now, I want to add ejb module for database access, I was looking on the internet but I didn't find something clear because it's my first time with EJB. I want to use something like @EJB in my controller like"

@Stateless(name = "CustomerServiceImpl")
public class CustomerServiceImpl implements CustomerService 


@EJB
private MyEjb myEjb;

and how can I configure it in spring context if there is a tutorial or any other help. It will be great and thank you

Brominate answered 11/4, 2014 at 9:34 Comment(0)
L
5

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

  1. 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.

  1. 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.

Lancer answered 10/5, 2014 at 7:32 Comment(1)
The First Way works for me! Thanks! But BEWARE that the LocalStatelessSessionProxyFactoryBean is misspelled in the first example. I had to face a ClassNotFoundException for a while until I realized it.Jarvisjary
K
3

Have a look here: http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#ejb-access-local

You can inject EJB using setter injection. Configure your bean this way:

<bean id="myComponent" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
    <property name="jndiName" value="ejb/myBean"/>
    <property name="businessInterface" value="com.mycom.MyComponent"/>
</bean>

<bean id="myController" class="com.mycom.myController">
    <property name="myComponent" ref="myComponent"/>
</bean>

You can also use <jee:local-slsb> tag to be able to inject your EJB:

<jee:local-slsb id="myComponent" jndi-name="ejb/myBean"
        business-interface="com.mycom.MyComponent"/>

<bean id="myController" class="com.mycom.myController">
    <property name="myComponent" ref="myComponent"/>
</bean>
Kingsley answered 11/4, 2014 at 15:18 Comment(1)
I'v tried this but jboss keep telling me that he can't find jndi name ... so i deleted this and i just used @EJB nothing more and everything works fine !!!! is that correct ? NB : my war use ejb module as dependencieBrominate

© 2022 - 2024 — McMap. All rights reserved.