如何使用 spring boot 1.x 配置 sessionListener [英] How to configure sessionListener use spring boot 1.x

查看:31
本文介绍了如何使用 spring boot 1.x 配置 sessionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Spring Boot. Now ,I want to add a listener.
e.g public MySessionListener implement HttpSessionListener
How to configure SpringApplication? Can I use SpringApplication.addListener() or other way to do it? Please.

推荐答案

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

What you are referring to are listeners for Spring context life-cycle. That is not what you want.

Spring 启动文档说明:

使用嵌入式 servlet 容器时,您可以注册 Servlet,过滤器和 Servlet 规范中的所有侦听器(例如HttpSessionListener) 直接作为 Spring bean.这可以是如果您想引用您的值,则特别方便配置期间的 application.properties.

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

这篇关于如何使用 spring boot 1.x 配置 sessionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆