依赖注入 servlet 侦听器 [英] dependency inject servlet listener
本文介绍了依赖注入 servlet 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 Stripes 应用中,我定义了以下类:
In my Stripes app I define the following class:
MyServletListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {
private SomeService someService;
private AnotherService anotherService;
// remaining implementation omitted
}
这个应用程序的服务层使用 Spring 在 XML 文件中定义和连接一些服务 bean.我想将实现 SomeService
和 AnotherService
的 bean 注入到 MyServletListener
中,这可能吗?
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?
推荐答案
这样的事情应该可行:
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
之后声明.
Your listener should be declared after Spring's ContextLoaderListener
in web.xml
.
这篇关于依赖注入 servlet 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文