避免在我的 Action 和 Service 类中使用 applicationContext.xml

发布于 2025-01-08 06:44:40 字数 594 浏览 5 评论 0原文

我正在使用 Struts、Spring 框架编写一个 Web 应用程序。 在 Struts Action 中,我按如下方式注入服务类

    ApplicationContext context =
            new ClassPathXmlApplicationContext("applicationContext.xml");
    loginService = (LoginService)context.getBean("loginService");

如何避免在我的 Action 类中提及 applicationContext.xml 我只需使用 loginService = (LoginService)context.getBean("loginService");没有在我的类中指定 .xml 文件。

时发现使用了下面的内容

我在谷歌搜索private LoginService loginService = (LoginService)ApplicationContextProvider.getContext().getBean("loginService");

;但我不想使用静态方法 getContext()。

I am writing a web application using Struts, Spring framework.
In the Struts Action I am Injecting Service Classes as follows

    ApplicationContext context =
            new ClassPathXmlApplicationContext("applicationContext.xml");
    loginService = (LoginService)context.getBean("loginService");

How can I avoid mentioning applicationContext.xml in my Action classes I just have to use
loginService = (LoginService)context.getBean("loginService"); without specifying the .xml file in my class.

I came across to use below while googling

private LoginService loginService = (LoginService)ApplicationContextProvider.getContext().getBean("loginService");

But I do not want to use static method getContext().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

瑶笙 2025-01-15 06:44:40

Spring 支持在包中集成 Struts org.springframework.web.struts

看一下这篇文章的示例。

Spring support Struts integration in the package org.springframework.web.struts.

Take a look at this article for an example.

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