在基于 EJB 的应用程序中加载 spring 上下文
以下是情况:
我有一个业务层,即一个EJB项目。事实上,只创建了一个 EJB。该 EJB 负责将服务类公开给调用 EJB 的其他层。我想在这一层引入spring(使用DI功能)。
我关心的是,在这个业务层中加载 spring 上下文的最佳方法是什么,这样每当调用 EJB 时,spring 上下文就不会一次又一次地加载?
(在Web项目中,在contextLoaderListener中配置spring上下文有一个优点,并且仅在应用程序启动时加载一次)
我想到将spring包含在同一层中,因为:
- 配置所有DAO和的依赖关系服务类并在必要时注入它们。
- 在业务层使用spring对hibernate的支持。
- 通过将属性注入类并对其进行模拟,可以轻松进行单元测试。不需要一次又一次地运行其他层来测试我的业务类/方法。
- 能够使用 AOP(面向方面编程)进行日志记录和方法级审计。
请帮我建议在 EJB 项目中加载 spring 上下文的最佳方法。我还想知道,如果我可以在应用程序服务器中加载相同的内容,是否有任何替代方案(我正在使用 Web sphere 应用程序服务器)。
感谢和问候,
Jitendriya Dash
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.
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 ?
(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)
I have thought of including spring in the same layer because :
- 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.
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).
Thanks and Regards,
Jitendriya Dash
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Spring 应该按照您通常设置的常规方式配置为应用程序的一部分。然后您需要从 EJB 层访问 Spring beans。访问(改编自这篇文章 ),按如下方式创建一个 Spring bean:
然后,从遗留应用程序调用有问题的 bean:
Spring 上下文初始化一次,您的 EJB 层可以随意访问。
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:
Then, to call the bean in question from the legacy app:
The Spring context is initialized once, and your EJB layer can access at will.
对于 EJB3,Spring 建议使用 EJB3 注入拦截器。基本上,您使用 ContextSingletonBeanFactoryLocator 需要在类路径中的 beanContextRef.xml 中创建 Spring 上下文。可能作为您 EAR 的一部分。 SpringBeanAutowiringInterceptor 将您的 bean 注入到您的 EJB 中。
For EJB3, Spring recommends using an EJB3 Injection Interceptor. Basically you specify your Spring context using a ContextSingletonBeanFactoryLocator which 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.
将 EJB 标记为单例 (
@Singleton
)。创建一次后,将 spring 上下文存储在该 bean 的变量中,以便您可以一次又一次返回相同的上下文。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.