Java EJB 3 注入 spring bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23008810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EJB 3 injection into spring beans
提问by elpazio
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"
我已经使用 spring、spring security 制作了一个 mavenized web 应用程序......现在,我想添加 ejb 模块用于数据库访问,我在互联网上寻找但我没有找到清楚的东西,因为这是我第一次使用 EJB。我想在我的控制器中使用类似 @EJB 的东西,比如“
@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
如果有教程或任何其他帮助,我如何在 spring 上下文中配置它。会很棒,谢谢
采纳答案by Vishal Akkalkote
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.
要将您的 ejb 3 bean 注入 spring bean,您可以按照以下步骤操作。1. 创建您的 Spring bean 2. 创建您的 EJB 及其远程和本地接口 3. 编写实现类,例如
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
上面是使用远程和本地接口的 EJB3 的例子
Now we create the Spring bean in which we inject this ejb.
现在我们创建 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;
}
}
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
我们在 spring 中添加了 ejb 的实例,但是我们需要在 spring 的 spring-config.xml 中注入它。在spring bean中注入ejb有2种方式
- 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.
注意:我在这里使用了本地界面,您可以根据需要使用远程。
- Another way of injecting the ejb is
- 注入 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" />
So when the bean get initialized at that time the ejb will get injected in your spring bean.
因此,当那时 bean 被初始化时,ejb 将被注入到您的 spring bean 中。
回答by Tomasz Nowak
Have a look here: http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#ejb-access-local
看看这里: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:
您可以使用 setter 注入来注入 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>
You can also use <jee:local-slsb>
tag to be able to inject your EJB:
您还可以使用<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>