spring session 导致 HttpSessionListener 失效

java 复制代码
@ServletComponentScan(basePackages = "com")
@EnableRedisHttpSession
public class StartAuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartAuthApplication.class, args);
    }
}


    @WebListener
    public static class MyHttpSessionListener implements HttpSessionListener {
        public MyHttpSessionListener() {
            System.out.println("MyHttpSessionListener");
        }

        @Override
        public void sessionCreated(HttpSessionEvent se) {
            log.info("sessionCreated new session {}", se.getSession().getId());
        }

        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            log.info("sessionDestroyed session destroyed {}", se.getSession().getId());
        }
    }

上面的代码,看上去没有什么问题吧。本意思是想通过ServletComponentScan+WebListener来添加一个监听器。但是 使用到了spring session (redis),会导致上面的代码, MyHttpSessionListener 里的代码不起作用,即 本文的主题 HttpSessionListener 失效。

其实原因也很简单,就是因为 HttpSerssionListener实现类,原本应该由servlet容调(比如TOMCAT)来调用,现在因为spring session的存在(可以理解为spring从中倒腾了下),调用顺序变成了 tomcat --> spring --> HttpSessionListener。 直接上代码

java 复制代码
public class SessionEventHttpSessionListenerAdapter
		implements ApplicationListener<AbstractSessionEvent>, ServletContextAware {

public void onApplicationEvent(AbstractSessionEvent event) {
		if (this.listeners.isEmpty()) {
			return;
		}

		HttpSessionEvent httpSessionEvent = createHttpSessionEvent(event);

        // 重点在这里 this.listeners。 这是spring代码里的一个对象
		for (HttpSessionListener listener : this.listeners) {
			if (event instanceof SessionDestroyedEvent) {
				listener.sessionDestroyed(httpSessionEvent);
			}
			else if (event instanceof SessionCreatedEvent) {
				listener.sessionCreated(httpSessionEvent);
			}
		}
	}

}

spring 抽象了一层 AbstractSessionEvent, this.listeners, 又是熟悉的套路,从这里可以看出,我们写的HttpSessionListener,变成了二等公民,由spring 的SessionEventHttpSessionListenerAdapter 这个 一等公民来决定什么时候调用。

所以,问题的根本原因,我们写的MyHttpSessionListener,之所以没有被调用,是因为spring的变量 this.listeners 没有得到我们写的实现类。继续想一下,spring 为什么没有拿到呢??

解决办法,把这个实现类,变成一个bean吧。

java 复制代码
    @WebListener
    // 添加一个注解就可以了
    @Component
    public static class MyHttpSessionListener implements HttpSessionListener {
    }

通过上面的分析能得出一个结论:由于spring的存在,原本本该由servlet直接调用我们的代码,有可能会变成由servlet 调用spring, 再由 spring 来调用我们的代码。而spring在调用我们的代码时候,它是怎们我们的代码的呢(比如某个接口的实现类有哪些),很有可能会以BEAN的形式进行查找某对象,从而调用对象的方法,原因也很好理解,毕意spring的另一个功能就是IOC,不难理解吧。

相关推荐
IT毕设实战小研3 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
wyiyiyi3 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
天宇_任4 小时前
Mysql数据库迁移到GaussDB注意事项
数据库·mysql·gaussdb
甄超锋4 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
武昌库里写JAVA6 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
xiep14383335107 小时前
Ubuntu 安装带证书的 etcd 集群
数据库·etcd
Java小白程序员7 小时前
Spring Framework:Java 开发的基石与 Spring 生态的起点
java·数据库·spring
老虎06278 小时前
数据库基础—SQL语句总结及在开发时
数据库·sql·oracle
甄超锋8 小时前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
还是鼠鼠9 小时前
tlias智能学习辅助系统--Maven 高级-私服介绍与资源上传下载
java·spring boot·后端·spring·maven