配置:Guice 1.0,Apache Tomcat 6.0

我目前正在使用此方法手动将Guice模块中配置的对象注入我的servlet:

public void init( ServletConfig config ) throws ServletException
{
    super.init( config );
    ServletContext sc = config.getServletContext();
    Injector injector = (Injector) sc
        .getAttribute( Constants.Guice.INJECTOR_APP_CONTEXT_KEY );
    injector.injectMembers( this );
}

如何在HttpSessionAttributeListener中执行相同的操作(因为它没有任何生命周期方法)?

有帮助吗?

解决方案

事件监听器是关于生命周期事件的。例如,在将属性添加到会话后立即调用 attributeAdded(),类似于Servlet的init()。

将对象注入HttpSession可能更有意义。在这种情况下,您需要在 HttpSessionListener.sessionCreated()中执行此操作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top