Gwt 使用guice servlet模块注册web.xml侦听器

Gwt 使用guice servlet模块注册web.xml侦听器,gwt,dependency-injection,guice,servlet-listeners,guice-servlet,Gwt,Dependency Injection,Guice,Servlet Listeners,Guice Servlet,我在我的项目中使用guice和guiceservlet。 我可以使用serve(…)和filter(…)方法在servletmodule中映射servlet和过滤器。 如何在servlet模块中注册侦听器(web.xml中的侦听器标记)。 我正在使用HttpSessionAttributeListener 我想在我的侦听器中使用guice注入器。我尝试使用bindListener(…),但似乎不起作用 关于基于guice的,ServletModule仅用于配置servlet和过滤器 此模块设置请

我在我的项目中使用guice和guiceservlet。 我可以使用serve(…)和filter(…)方法在servletmodule中映射servlet和过滤器。 如何在servlet模块中注册侦听器(web.xml中的侦听器标记)。 我正在使用HttpSessionAttributeListener

我想在我的侦听器中使用guice注入器。我尝试使用bindListener(…),但似乎不起作用

关于基于guice的

ServletModule
仅用于配置servlet和过滤器

此模块设置请求和会话范围,并提供配置筛选器和servlet的位置。


因此,在您的情况下,您必须将侦听器添加到
web.xml
,并以某种方式获取注入器。

我可以想出一些选项

(1) 将侦听器注册为普通(在web.xml中),并从servlet上下文属性检索注入器
GuiceServletContextListener
初始化后,使用属性名
injector.class.getName()
将injector实例放入servlet上下文中。我不确定这是否有文档记录或支持,因此您可能希望为注入器定义自己的属性名称,并将其放在那里。只要确保您考虑了侦听器的初始化顺序——在调用GuiceServletContextListener之前,不会绑定注入器

class MyListenerExample implement HttpSessionListener { // or whatever listener
    static final String INJECTOR_NAME = Injector.class.getName();

    public void sessionCreated(HttpSessionEvent se) {
        ServletContext sc = se.getSession().getServletContext();
        Injector injector = (Injector)sc.getAttribute(INJECTOR_NAME);
        // ...
    }
}
(2) 如果您使用的是Java Servlets API版本3.0+,则可以在ServletContext上调用
addListener
。我可能会建议您在创建注入器时正确地执行此操作,尽管您可以在任何地方执行此操作。这种方法让我觉得有点老套,但应该能奏效

public class MyServletConfig extends GuiceServletContextListener {
    ServletContext servletContext;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        servletContext = event.getServletContext();

        // the super call here is where Guice Servlets calls
        // getInjector (below), binds the ServletContext to the
        // injector and stores the injector in the ServletContext.
        super.contextInitialized(event);
    }

    @Override
    protected Injector getInjector() {
        Injector injector = Guice.createInjector(new MyServletModule());
        // injector.getInstance(ServletContext.class) <-- won't work yet!

        // BIND HERE, but note the ServletContext will not be in the injector
        servletContext.addListener(injector.getInstance(MyListener.class));
        // (or alternatively, store the injector as a member field and
        // bind in contextInitialized above, after the super call)

        return injector;
    }
}
公共类MyServletConfig扩展了GuiceServletContextListener{
ServletContext ServletContext;
@凌驾
公共void contextInitialized(ServletContextEvent事件){
servletContext=event.getServletContext();
//这里的超级调用是Guice Servlets调用的地方
//getInjector(如下)将ServletContext绑定到
//喷油器,并将喷油器存储在ServletContext中。
super.contextInitialized(事件);
}
@凌驾
受保护的喷油器getInjector(){
Injector=Guice.createInjector(新的MyServletModule());

//injector.getInstance(ServletContext.class)非常有用!就我而言,在
contextInitialized()
之前调用
getInjector()
。这有点奇怪,但仍然有效!