EJB 3 injections in spring beans

I made a mavenized web application with spring, spring security ... Now I want to add an ejb module to access the database, I looked on the Internet, but I did not find something clearly, because this is my first time with EJB. I want to use something like @EJB in my controller as "

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


@EJB
private MyEjb myEjb;

and how to configure it in the context of spring, if there is a tutorial or any other help. It will be great and thanks.

+4
source share
2 answers

To introduce an ejb 3 bean into a spring bean, you can follow these steps. 1. Create a spring bean 2. Create your EJB using a remote and local interface 3. Write an implementation class for example.

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";   
 }
}

EJB3, ,

spring bean, ejb.

package com.ejb;

@Service
public class MyService {

   private MyEjbLocal ejb;

   public void setMyEjbLocal(MyEjbLocal ejb){
        this.ejb = ejb;
  }

  public MyEjbLocal getMyEjbLocal(){
       return ejb;
  }
}

ejb spring, spring spring-config.xml. ejb spring bean

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

. , Remote .

  1. ejb
<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" />

, bean , ejb spring bean.

+5

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

EJB . bean :

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

<jee:local-slsb>, 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>
+3

Source: https://habr.com/ru/post/1536074/


All Articles