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

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

问题描述

我已经制作了一个带有弹簧,弹簧安全性的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

推荐答案

要在spring bean中注入你的ejb 3 bean,你可以按照以下步骤操作。
1.创建你的Spring bean
2.创建你的EJB它的远程和本地接口
3.写实现类
例如

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

我们在春天添加了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. 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>

注意:我在这里使用了本地界面您可以根据需要使用Remote。

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天全站免登陆