EJB 3 注入 spring bean [英] EJB 3 injection into spring beans

查看:32
本文介绍了EJB 3 注入 spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 spring、spring security 做了一个 mavenized web 应用程序...现在,我想添加 ejb 模块来访问数据库,我在互联网上寻找但我没有找到清楚的东西,因为这是我第一次与 EJB.我想在我的控制器中使用类似 @EJB 的东西喜欢"

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;

如果有教程或任何其他帮助,我如何在 spring 上下文中配置它.会很棒,谢谢

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

推荐答案

要将 ejb 3 bean 注入 spring bean,您可以按照以下步骤操作.1. 创建你的 Spring bean2. 使用远程和本地接口创建 EJB3. 编写实现类例如

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

以上是使用远程和本地接口的EJB3示例

above is the example of EJB3 which uses both remote and local interface

现在我们创建 Spring bean,在其中注入这个 ejb.

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

我们在 spring 中添加了 ejb 的实例,但是我们需要在 spring 的 spring-config.xml 中注入它.spring bean注入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. 第一种方式

<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. 另一种注入 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 中.

So when the bean get initialized at that time the ejb will get injected in your spring bean.

这篇关于EJB 3 注入 spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆