小编典典

如何使用Spring Boot 1.x配置SessionListener

spring-boot

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


阅读 389

收藏
2020-05-30

共1个答案

小编典典

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

Spring Boot文档指出

使用嵌入式Servlet容器时,您可以直接将Servlet规范中的Servlet,过滤器和所有侦听器(例如HttpSessionListener)注册为Spring
Bean。如果要在配置过程中引用application.properties中的值,这可能特别方便。

更新:

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(); 
}
2020-05-30