【问题标题】:Injecting an object into a HttpSessionAttributeListener via Guice?通过 Guice 将对象注入 HttpSessionAttributeListener?
【发布时间】:2009-09-10 08:11:12
【问题描述】:

配置: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 中做同样的事情(因为它没有任何生命周期方法)?

【问题讨论】:

    标签: java tomcat servlets dependency-injection guice


    【解决方案1】:

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

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

    【讨论】:

    • HttpSessionListener 和 HttpSessionAttributeListener 有什么区别?
    • 一个监听会话的事件,另一个监听会话中的单个属性。
    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多