使用Guice将依赖项注入ServletContextListener [英] Injecting dependencies to ServletContextListener with Guice
本文介绍了使用Guice将依赖项注入ServletContextListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于ServletContextListener是由服务器而不是Guice创建的,所以我找不到使它协同工作的方法。
Since ServletContextListener is created by the server, not by Guice I can't find a way to make it work together. How do I get guice injector at ServletContextListener?
也许有更好的方法关闭诸如logger或持久性之类的服务,然后在contextDestroyed方法中进行设置并在contextInitialized中对其进行初始化? p>
Maybe there is better way to shutdown services like logger or persistance then doing it at contextDestroyed method and initialize them at contextInitialized?
推荐答案
扩展名 GuiceServlet 将注入器置于servlet上下文中,因此您可以通过执行以下操作来获得它:
The extension GuiceServlet puts the injector in the servlet context, so you can get it by doing something like this:
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
Injector injector = (Injector) sce.getServletContext()
.getAttribute(Injector.class.getName());
}
}
这篇关于使用Guice将依赖项注入ServletContextListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文