简体   繁体   English

如何使用Spring 2.5将资源注入EJB3 bean?

[英]How to inject resources into EJB3 beans with Spring 2.5?

如果我在使用Spring 2.5 for DI的应用程序中创建EJB3 bean(比如无状态会话bean),我应该如何将Spring中的依赖项注入bean而不将bean耦合到Spring?

I don't know if you consider applying an interceptor as coupling but that's to my knowledge the standard approach. 我不知道您是否考虑将拦截器应用为耦合,但据我所知,这是标准方法。 From the Chapter 18. Enterprise Java Beans (EJB) integration of the documentation: 第18章企业Java Bean(EJB)文档集成

18.3.2. 18.3.2。 EJB 3 injection interceptor EJB 3注入拦截器

For EJB 3 Session Beans and Message-Driven Beans, Spring provides a convenient interceptor that resolves Spring 2.5's @Autowired annotation in the EJB component class: org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor . 对于EJB 3会话Bean和消息驱动Bean,Spring提供了一个方便的拦截器,可以解析EJB组件类中的Spring 2.5的@Autowired注释: org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor This interceptor can be applied through an @Interceptors annotation in the EJB component class, or through an interceptor-binding XML element in the EJB deployment descriptor. 此拦截器可以通过EJB组件类中的@Interceptors批注应用,也可以通过EJB部署描述符中的拦截器绑定XML元素应用。

 @Stateless @Interceptors(SpringBeanAutowiringInterceptor.class) public class MyFacadeEJB implements MyFacadeLocal { // automatically injected with a matching Spring bean @Autowired private MyComponent myComp; // for business method, delegate to POJO service impl. public String myFacadeMethod(...) { return myComp.myMethod(...); } ... } 

SpringBeanAutowiringInterceptor by default obtains target beans from a ContextSingletonBeanFactoryLocator , with the context defined in a bean definition file named beanRefContext.xml . 默认情况下, SpringBeanAutowiringInterceptorContextSingletonBeanFactoryLocator获取目标bean,并在名为beanRefContext.xml的bean定义文件中定义beanRefContext.xml By default, a single context definition is expected, which is obtained by type rather than by name. 默认情况下,需要单个上下文定义,该定义是按类型而不是按名称获取的。 However, if you need to choose between multiple context definitions, a specific locator key is required. 但是,如果需要在多个上下文定义之间进行选择,则需要特定的定位器键。 The locator key (ie the name of the context definition in beanRefContext.xml ) can be explicitly specified either through overriding the getBeanFactoryLocatorKey method in a custom SpringBeanAutowiringInterceptor subclass. 可以通过覆盖自定义SpringBeanAutowiringInterceptor子类中的getBeanFactoryLocatorKey方法来显式指定定位器键(即beanRefContext.xml中的上下文定义的beanRefContext.xml )。

The only other option I'm aware of (extending the EJB 2.x support classes) is much worse from a coupling point of view (and thus doesn't answer your question). 从耦合的角度来看,我所知道的唯一另一个选项(扩展EJB 2.x支持类)要差得多(因此不能回答你的问题)。

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM