How to inject dependencies into HttpSessionListener, using Spring?

前端 未结 3 1911
春和景丽
春和景丽 2020-11-30 08:41

How to inject dependencies into HttpSessionListener, using Spring and without calls, like context.getBean(\"foo-bar\") ?

3条回答
  •  广开言路
    2020-11-30 09:28

    You can declare your HttpSessionListener as a bean in Spring context, and register a delegation proxy as an actual listener in web.xml, something like this:

    public class DelegationListener implements HttpSessionListener {
        public void sessionCreated(HttpSessionEvent se) {
            ApplicationContext context = 
                WebApplicationContextUtils.getWebApplicationContext(
                    se.getSession().getServletContext()
                );
    
            HttpSessionListener target = 
                context.getBean("myListener", HttpSessionListener.class);
            target.sessionCreated(se);
        }
    
        ...
    }
    

提交回复
热议问题