BERARM BERARM - 25 days ago 7
Java Question

How to configure sessionListener use spring boot 1.x

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.

Answer

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

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.

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