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

相关推荐
Coder_Boy_15 小时前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
热爱专研AI的学妹16 小时前
数眼搜索API与博查技术特性深度对比:实时性与数据完整性的核心差异
大数据·开发语言·数据库·人工智能·python
hopsky16 小时前
ShardingSphere功能简介
数据库·sql
talenteddriver16 小时前
mysql: MySQL索引和排序相关名词概念汇总
数据库·mysql
武昌库里写JAVA16 小时前
iview-CRUD模板
vue.js·spring boot·sql·layui·课程设计
6极地诈唬16 小时前
【PG漫步】DELETE不会改变本地文件的大小,VACUUM也不会
linux·服务器·数据库
小许学java17 小时前
Spring原理
java·spring·生命周期·作用域·原理
MZWeiei17 小时前
Redis持久化机制中的 AOF机制简单介绍
数据库·redis
Elastic 中国社区官方博客18 小时前
Elasticsearch:在 X-mas 吃一些更健康的东西
android·大数据·数据库·人工智能·elasticsearch·搜索引擎·全文检索
酷柚易汛18 小时前
酷柚易汛ERP 2025-12-26系统升级日志
java·前端·数据库·php