如何配置sessionListener use spring boot 1.x

How to configure sessionListener use spring boot 1.x

我是 Spring Boot 的新手。 现在,我想添加一个监听器。
例如public MySessionListener implement HttpSessionListener
如何配置SpringApplication? 我可以使用 SpringApplication.addListener() 或其他方式吗?请。

您指的是 Spring 上下文生命周期的侦听器。那不是你想要的。

Spring boot documentation states:

When using an embedded servlet container you can register Servlets, Filters and all the listeners from the Servlet spec (e.g. HttpSessionListener) directly as Spring beans. This can be particularly convenient if you want to refer to a value from your application.properties during configuration.

更新:

import org.springframework.context.annotation.Bean;
import javax.servlet.http.HttpSessionListener;

@Bean
public HttpSessionListener httpSessionListener(){
    // MySessionListener should implement javax.servlet.http.HttpSessionListener
    return new MySessionListener(); 
}