Spring Boot配置Session监听无效。

我有一个spring boot (v1.5.9.RELEASE)项目,想要对session进行管理/监听,实现了HttpSessionListener和HttpSessionAttributeListener这两个接口,在MySessionListener中使用@WebListener注解,在启动类中使用@SpringBootApplication注解
但是经过测试,访问网页(ftl页面)无法触发sessionCreated()方法,session过期时也无法触发sessionDestroyed()方法;用户登陆时调用request.getSession().setAttribute()或者session().setAttribute()都无法触发attributeAdded()方法。

程序启动时可以输出构造方法中的内容
2018-01-22 18:02:49.761 INFO 35600 --- [ost-startStop-1] c.t.x.c.listener.MySessionListener : MySessionListenerInitialized

请问有人知道这是为什么吗

代码如下:

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);
        // 程序启动时可以在控制台中输出这句话
        public MySessionListener() { 
            log.info("MySessionListenerInitialized"); 
        }
        @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());
        }
    } 

还有就是使用springbean的配置方式同样无效

    @Configuration
    public class MyWebConfiguration extends WebMvcConfigurerAdapter {
        @Bean
        public ServletListenerRegistrationBean<EventListener> sessionListener() {
            ServletListenerRegistrationBean<EventListener> sessionListener = new ServletListenerRegistrationBean<>();
            sessionListener.setListener(new MySessionListener());
            return sessionListener;
        }
    }

怎么没看到你写的判断分支语句?

1.你可以先看一下你的监听有没有进行实例化
2.如果没有实例化有问题就很正常了,这时候你需要配置一下你包的扫描路径
希望可以帮到你

1。在MySessionListener 中加个初始化块,看启动时能不能打印出来
{

System.out.println("初始化块");

}
2。在setAttribute()方法前面打印点东西,确认这个方法是否真的执行了

在controller层通过,request.getSession()操作,触发sessionCreated()方法;
request.getSession().invalidate()触发sessionDestroyed()方法,不过销毁的触发还有其他方式如session的自动过期等

我是好心人

springBoot集成springSession导致 session监听器失效问题解决
https://blog.csdn.net/baidu_38307690/article/details/80199539
实测可行

代码其实可以这样写
1. 先继承ttpSessionListener {
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

//@WebListener
public class CustomListener implements HttpSessionListener {

public CustomListener() {
    System.out.println("CustomListener init:");
}

@Override
public void sessionCreated(HttpSessionEvent se) {
    HttpSessionListener.super.sessionCreated(se);
    System.out.println("sessionID:" + se.getSession().getId());
}

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    HttpSessionListener.super.sessionDestroyed(se);
}

}

2.配置监听器
import java.util.EventListener;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.util.IntrospectorCleanupListener;

import com.listener.CustomListener;

@Configuration
public class SessionConfig {

@Bean
public ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean() {
    ServletListenerRegistrationBean<EventListener> srb = new ServletListenerRegistrationBean<EventListener>();
    //防止Spring内存溢出监听器
    srb.setListener(new IntrospectorCleanupListener());
    //request监听器 主要需要配置这个监听器
    srb.setListener(new RequestContextListener());
    srb.setListener(new CustomListener());
    return srb;
}

}


楼主可解决了?我也遇到这个问题,我的session是存在redis里的,不知道是不是这个问题导致不能监听的?