依赖注入 servlet 侦听器

发布于 2024-10-28 03:49:03 字数 452 浏览 0 评论 0原文

在我的 Stripes 应用程序中,我定义了以下类:

MyServletListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

  private SomeService someService;

  private AnotherService anotherService;

  // remaining implementation omitted
} 

该应用程序的服务层使用 Spring 在 XML 文件中定义一些服务 bean 并将其连接在一起。我想将实现 SomeServiceAnotherService 的 Bean 注入到 MyServletListener 中,这可能吗?

In my Stripes app I define the following class:

MyServletListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

  private SomeService someService;

  private AnotherService anotherService;

  // remaining implementation omitted
} 

The service layer of this app uses Spring to define and wire together some service beans in an XML file. I would like to inject the beans that implement SomeService and AnotherService into MyServletListener, is this possible?

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

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

发布评论

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

评论(2

疯了 2024-11-04 03:49:03

像这样的事情应该有效:

public class MyServletListener implements ServletContextListener, HttpSessionAttributeListener, HttpSessionListener {
    @Autowired
    private SomeService someService;        
    @Autowired
    private AnotherService anotherService; 

    public void contextInitialized(ServletContextEvent sce) {
        WebApplicationContextUtils
            .getRequiredWebApplicationContext(sce.getServletContext())
            .getAutowireCapableBeanFactory()
            .autowireBean(this);
    }

    ...
}

您的侦听器应该在 web.xml 中的 Spring 的 ContextLoaderListener 之后声明。

Something like this should work:

public class MyServletListener implements ServletContextListener, HttpSessionAttributeListener, HttpSessionListener {
    @Autowired
    private SomeService someService;        
    @Autowired
    private AnotherService anotherService; 

    public void contextInitialized(ServletContextEvent sce) {
        WebApplicationContextUtils
            .getRequiredWebApplicationContext(sce.getServletContext())
            .getAutowireCapableBeanFactory()
            .autowireBean(this);
    }

    ...
}

Your listener should be declared after Spring's ContextLoaderListener in web.xml.

向地狱狂奔 2024-11-04 03:49:03

使用 SpringBeanAutowiringSupport 类更短更简单。
您所要做的就是这样:

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

因此使用 axtavt 中的示例:

public class MyServletListener implements ServletContextListener, HttpSessionAttributeListener, HttpSessionListener {
    @Autowired
    private SomeService someService;        
    @Autowired
    private AnotherService anotherService; 

    public void contextInitialized(ServletContextEvent sce) {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    ...
}

Little bit shorter and simpler is to use SpringBeanAutowiringSupport class.
Than all you have to do is this:

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

So using example from axtavt:

public class MyServletListener implements ServletContextListener, HttpSessionAttributeListener, HttpSessionListener {
    @Autowired
    private SomeService someService;        
    @Autowired
    private AnotherService anotherService; 

    public void contextInitialized(ServletContextEvent sce) {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

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