EJB 3 injection into spring beans

前端 未结 2 1889
无人共我
无人共我 2020-12-10 18:41

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

相关标签:
2条回答
  • 2020-12-10 19:16

    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.

    0 讨论(0)
  • 2020-12-10 19:26

    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>
    
    0 讨论(0)
提交回复
热议问题