本文主要是介绍spring session 导致 HttpSessionListener 失效,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
@ServletComponentScan(basePackages = "com")
@EnableRedisHttpSession
public class StartAuthApplication {public static void main(String[] args) {SpringApplication.run(StartAuthApplication.class, args);}
}@WebListenerpublic static class MyHttpSessionListener implements HttpSessionListener {public MyHttpSessionListener() {System.out.println("MyHttpSessionListener");}@Overridepublic void sessionCreated(HttpSessionEvent se) {log.info("sessionCreated new session {}", se.getSession().getId());}@Overridepublic 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。 直接上代码
public class SessionEventHttpSessionListenerAdapterimplements 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吧。
@WebListener// 添加一个注解就可以了@Componentpublic static class MyHttpSessionListener implements HttpSessionListener {}
通过上面的分析能得出一个结论:由于spring的存在,原本本该由servlet直接调用我们的代码,有可能会变成由servlet 调用spring, 再由 spring 来调用我们的代码。而spring在调用我们的代码时候,它是怎们我们的代码的呢(比如某个接口的实现类有哪些),很有可能会以BEAN的形式进行查找某对象,从而调用对象的方法,原因也很好理解,毕意spring的另一个功能就是IOC,不难理解吧。
这篇关于spring session 导致 HttpSessionListener 失效的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!