java 在基于 EJB 的应用程序中加载 spring 上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7885014/
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
Load spring context in an EJB based application
提问by dash27
The following is the situation :
以下是情况:
I have a business layer, that is an EJB project. In fact, there is only one EJB that is created. This EJB is responsible to expose the service classes to other layers, that calls the EJB. I want to introduce spring (to use DI feature) in this layer.
我有一个业务层,即 EJB 项目。实际上,只创建了一个 EJB。该 EJB 负责向调用 EJB 的其他层公开服务类。我想在这一层引入 spring(使用 DI 功能)。
My concern is, what is the best way to load the spring context in this business layer, so that the spring context does not get loaded again and again, whenever the EJB gets called ?
我担心的是,在此业务层中加载 spring 上下文的最佳方法是什么,以便每次调用 EJB 时都不会一次又一次地加载 spring 上下文?
(In a Web project, there is an advantage rather to configure the spring context in contextLoaderListener, and it gets loaded once only when the application gets started)
(在Web项目中,比在contextLoaderListener中配置spring上下文有一个好处,它只在应用程序启动时加载一次)
I have thought of including spring in the same layer because :
我曾考虑在同一层中包含 spring ,因为:
- Configure the dependencies of all DAO and service classes and inject them wherever necessary.
- To use spring support for hibernate in the business layer.
- Ease of Unit testing, by injecting the properties into classes and simulating the same. Don't need to run the other layers again and again, to test my business classes/methods.
- To be able to use AOP (Aspect Oriented Programming) for Logging and method level auditing.
- 配置所有 DAO 和服务类的依赖项,并在必要时注入它们。
- 在业务层使用spring支持hibernate。
- 易于单元测试,通过将属性注入类并模拟相同。不需要一次又一次地运行其他层,来测试我的业务类/方法。
- 能够使用 AOP(面向方面的编程)进行日志记录和方法级审计。
Kindly help me suggesting the best way, to load the spring context in an EJB project. I also want to know , if there are any alternatives if I can load the same in the app server (I am using Web sphere app server).
请帮我建议最好的方法,在 EJB 项目中加载 spring 上下文。我还想知道,如果我可以在应用程序服务器中加载相同的内容(我正在使用 Web Sphere 应用程序服务器),是否还有其他选择。
Thanks and Regards,
谢谢并恭祝安康,
Jitendriya Dash
吉腾德里亚冲刺
回答by atrain
Spring should be configured as part of your application in the normal way that you always set it up. Then you need to access the Spring beans from the EJB layer. To access (adapted from this post), create a Spring bean as follows:
Spring 应该以您始终设置它的正常方式配置为您的应用程序的一部分。然后您需要从 EJB 层访问 Spring bean。要访问(改编自这篇文章),请创建一个 Spring bean,如下所示:
@Component
public class SpringApplicationContext implements ApplicationContextAware {
private static ApplicationContext CONTEXT;
public void setApplicationContext(ApplicationContext context) throws BeansException {
CONTEXT = context;
}
public static Object getBean(String beanName) {
return CONTEXT.getBean(beanName);
}
}
Then, to call the bean in question from the legacy app:
然后,从遗留应用程序调用有问题的 bean:
SomeService someService = (SomeService)SpringApplicationContext.getBean("someServiceImpl");
The Spring context is initialized once, and your EJB layer can access at will.
Spring 上下文初始化一次,您的 EJB 层可以随意访问。
回答by Bill Poitras
For EJB3, Spring recommends using an EJB3 Injection Interceptor. Basically you specify your Spring context using a ContextSingletonBeanFactoryLocatorwhich entails creating your Spring context in a beanContextRef.xml in your classpath. Probably as part of your EAR. The SpringBeanAutowiringInterceptor injects your bean into your EJB.
对于 EJB3,Spring 建议使用EJB3 Injection Interceptor。基本上,您使用ContextSingletonBeanFactoryLocator指定您的 Spring 上下文,这需要在类路径中的 beanContextRef.xml 中创建您的 Spring 上下文。可能作为您 EAR 的一部分。SpringBeanAutowiringInterceptor 将您的 bean 注入您的 EJB。
回答by Ralph
Marking the EJB been as a Singleton (@Singleton
). And storing the spring context in a variable in this bean, after it is created once, so that you can return the same context again and again.
将 EJB 标记为单例 ( @Singleton
)。并在创建一次后将 spring 上下文存储在此 bean 中的变量中,以便您可以一次又一次地返回相同的上下文。