在基于 EJB 的应用程序中加载 spring 上下文

发布于 2024-12-12 04:16:06 字数 622 浏览 0 评论 0原文

以下是情况:

我有一个业务层,即一个EJB项目。事实上,只创建了一个 EJB。该 EJB 负责将服务类公开给调用 EJB 的其他层。我想在这一层引入spring(使用DI功能)。

我关心的是,在这个业务层中加载 spring 上下文的最佳方法是什么,这样每当调用 EJB 时,spring 上下文就不会一次又一次地加载?

(在Web项目中,在contextLoaderListener中配置spring上下文有一个优点,并且仅在应用程序启动时加载一次)

我想到将spring包含在同一层中,因为:

  1. 配置所有DAO和的依赖关系服务类并在必要时注入它们。
  2. 在业务层使用spring对hibernate的支持。
  3. 通过将属性注入类并对其进行模拟,可以轻松进行单元测试。不需要一次又一次地运行其他层来测试我的业务类/方法。
  4. 能够使用 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 :

  1. Configure the dependencies of all DAO and service classes and inject them wherever necessary.
  2. To use spring support for hibernate in the business layer.
  3. 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.
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心意如水 2024-12-19 04:16:06

Spring 应该按照您通常设置的常规方式配置为应用程序的一部分。然后您需要从 EJB 层访问 Spring beans。访问(改编自这篇文章 ),按如下方式创建一个 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);
    }
}

然后,从遗留应用程序调用有问题的 bean:

SomeService someService = (SomeService)SpringApplicationContext.getBean("someServiceImpl");

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:

@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:

SomeService someService = (SomeService)SpringApplicationContext.getBean("someServiceImpl");

The Spring context is initialized once, and your EJB layer can access at will.

浸婚纱 2024-12-19 04:16:06

对于 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.

一刻暧昧 2024-12-19 04:16:06

将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文