Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB 3 injection into spring beans

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

like image 777
elpazio Avatar asked Apr 11 '14 09:04

elpazio


People also ask

Can we use EJB with Spring?

However, it is important to note that using Spring does not prevent you from using EJBs. In fact, Spring makes it much easier to access EJBs and implement EJBs and functionality within them.

How do you inject beans in Spring core?

The injection in Spring is either done via setter, field or constructor injection. Classes which are managed by Spring DI must conform to the Java bean standard. In the context of Spring classes are also referred to as beans or as spring beans.

Can @bean method be private?

Any @Bean annotated method, which is not public (i.e. with protected, private and default visibility), will create a 'hidden' bean. In the example above, mainBean has been configured with both publicBean and hiddenBean.


2 Answers

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.

like image 159
Vishal Akkalkote Avatar answered Sep 18 '22 10:09

Vishal Akkalkote


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>
like image 25
Tomasz Nowak Avatar answered Sep 18 '22 10:09

Tomasz Nowak