我有一个spring boot (v1.5.9.RELEASE)项目,想要实现session的管理,实现了HttpSessionListener和HttpSessionAttributeListener这两个接口,在MySessionListener中使用@WebListener注解,在启动类中使用@SpringBootApplication注解
但是经过测试,访问网页(ftl页面)无法触发sessionCreated()方法;用户登陆时调用request.getSession().setAttribute()或者session().setAttribute()都无法触发attributeAdded()方法。请问有人知道这是为什么吗代码如下:

解决方案 »

  1.   

    重发一下代码部分DemoApplication.java    @SpringBootApplication
        @ServletComponentScan
        public class DemoApplication {
                public static void main(String[] args) {
                        SpringApplication.run(DemoApplication.class, args);
                }
        }MySessionListener.java    @WebListener
        public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {
            private final static Log log = LogFactory.getLog(MySessionListener.class);
            @Override
            public void sessionCreated(HttpSessionEvent se) {
                log.info("sessionCreated-----" + se.getSession().getId());
            }
            @Override
            public void sessionDestroyed(HttpSessionEvent se) {
                log.info("sessionDestroyed-----" + se.getSession().getId());
            }
            @Override
            public void attributeAdded(HttpSessionBindingEvent se) {
                log.info("attributeAdded: "+se.getSession().getId());
            }
            @Override
            public void attributeRemoved(HttpSessionBindingEvent se) {
                log.info("attributeRemoved: "+se.getSession().getId());
            }
            @Override
            public void attributeReplaced(HttpSessionBindingEvent se) {
                log.info("attributeReplaced: "+se.getSession().getId());
            }
        } 
      

  2.   

    MySessionListener需要注册吧,
     @Bean
        public ServletRegistrationBean indexServletRegistration() {
            ServletRegistrationBean registration = new ServletRegistrationBean(new ClassName());
            registration.addUrlMappings("/XXX");
            return registration;
        }