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,不难理解吧。

相关推荐
TDengine (老段)21 小时前
TDengine IDMP 可视化 —— 分享
大数据·数据库·人工智能·时序数据库·tdengine·涛思数据·时序数据
布局呆星21 小时前
SpringBoot 基础入门
java·spring boot·spring
GottdesKrieges1 天前
OceanBase数据库备份配置
数据库·oceanbase
SPC的存折1 天前
MySQL 8组复制完全指南
linux·运维·服务器·数据库·mysql
运维行者_1 天前
OpManager MSP NetFlow Analyzer集成解决方案,应对多客户端网络流量监控挑战
大数据·运维·服务器·网络·数据库·自动化·运维开发
炸炸鱼.1 天前
Python 操作 MySQL 数据库
android·数据库·python·adb
softshow10261 天前
Etsy 把 1000 个 MySQL 分片迁进 Vitess
数据库·mysql
Ronaldinho Gaúch1 天前
MySQL基础
数据库·mysql
不剪发的Tony老师1 天前
Noir:一款键盘驱动的现代化数据库管理工具
数据库·sql
❀͜͡傀儡师1 天前
Spring AI Alibaba vs. AgentScope:两个阿里AI框架,如何选择?
java·人工智能·spring