Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
156 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...