【问题标题】:How to configure sessionListener use spring boot 1.x如何配置 sessionListener 使用 spring boot 1.x
【发布时间】:2015-05-10 09:22:59
【问题描述】:

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

【问题讨论】:

    标签: java spring spring-mvc servlets spring-boot


    【解决方案1】:

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

    Spring boot documentation states:

    使用嵌入式 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(); 
    }
    

    【讨论】:

    • 感谢您的回答,我误解了 addListener 方法。我会试试你的建议。
    • @telebog。添加了如何注册 Spring bean 的简单示例
    • 你错过了该方法的回报
    • 这拯救了我的一天!谢谢!
    猜你喜欢
    • 2022-01-07
    • 2014-10-04
    • 2016-07-04
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2017-05-17
    • 2017-07-18
    相关资源
    最近更新 更多