简体   繁体   English

如何配置sessionListener使用spring boot 1.x.

[英]How to configure sessionListener use spring boot 1.x

I'm new to Spring Boot. 我是Spring Boot的新手。 Now ,I want to add a listener. 现在,我想添加一个监听器。
eg public MySessionListener implement HttpSessionListener 例如, public MySessionListener implement HttpSessionListener
How to configure SpringApplication ? 如何配置SpringApplication Can I use SpringApplication.addListener() or other way to do it? 我可以使用SpringApplication.addListener()或其他方法吗? Please. 请。

What you are referring to are listeners for Spring context life-cycle. 你所指的是Spring上下文生命周期的监听器。 That is not what you want. 那不是你想要的。

Spring boot documentation states : Spring启动文档说明

When using an embedded servlet container you can register Servlets, Filters and all the listeners from the Servlet spec (eg HttpSessionListener) directly as Spring beans. 使用嵌入式servlet容器时,您可以将Servlet规范中的Servlet,过滤器和所有侦听器(例如HttpSessionListener)直接注册为Spring bean。 This can be particularly convenient if you want to refer to a value from your application.properties during configuration. 如果要在配置期间引用application.properties中的值,这可能特别方便。

UPDATE: 更新:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM