From SpringBootServletInitializer
javadoc : A handy opinionated WebApplicationInitializer for applications that only have one Spring servlet, and no more than a single filter (which itself is only enabled when Spring Security is detected). If your application is more complicated consider using one of the other WebApplicationInitializers
So if you want to generate a war and you want to include a session listener, you should use directly a WebApplicationInitializer
. Here is an example from the javadoc :
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
As you have full control on the ServletContext before it is fully initialized, it is easy to add your listener :
container.addListener(YourListener.class);