将EJB 3注入Spring Bean [英] Injecting EJB 3 into Spring Bean
问题描述
我正在尝试将EJB注入Spring(3.1.2)服务(均在不同 WAR中) 两者都很简单(删除了方法以简化示例):
I am trying to inject EJB into Spring (3.1.2) service (both in different WARs) Both are very simple (methods removed to simplify example):
EJB:
@Remote
public interface MyBean {
}
@Singleton
public class MyBeanImpl implements MyBean{
}
服务:
@Service
public class MyServiceImpl implements MyService{
}
乍一看,事情很简单,但我尝试过:
At first sight thing is very simple, but I tried:
@EJB(lookup = "java:global/ejbApp/MyBeanImpl!com.my.MyBean")
private MyBean myBean;
,它不起作用.然后我也尝试了:
and it didin't work. Then I also tried:
@EJB(mappedName = "java:global/ejbApp/MyBeanImpl!com.my.MyBean")
private MyBean myBean;
还有
@Resource(mappedName = "java:global/ejbApp/MyBeanImpl!com.my.MyBean")
private MyBean myBean;
但都不起作用.
我设法使用以下方法注入了EJB:
I managed to inject my EJB using:
<jee:jndi-lookup id="myBean" jndi-name="java:global/ejbApp/MyBeanImpl!com.my.MyBean" />
在我的spring配置和服务中:
in my spring configuration and in the service:
@Autowired
private MyBean myBean;
但是我真的不喜欢这种解决方案.我想在 some 批注中添加我的JNDI路径,例如:
But I really dont like this solution. I would like to have my JNDI path in some annotation to be able to do e.g:
@EJB(lookup = MyBean.JNDI_NAME)
private MyBean myBean;
推荐答案
我们已经找到了非常简单的解决方案. 放入spring配置文件中,必须放入:
We have found quite nice and simple solution. Into spring configuration file one has to put:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
<property name="alwaysUseJndiLookup" value="true" />
</bean>
这使spring可以在JNDI中搜索用@Resource注释的 beans . 所以现在可以做到:
And that enables spring to search for the beans annotated with @Resource in JNDI. So now one can do:
@Resource(mappedName = MyBean.JNDI_NAME)
private MyBean myBean;
这篇关于将EJB 3注入Spring Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!