简体   繁体   中英

How to register ServletContextListener in spring boot

Hello I'm trying to rewrite my old code to use Spring Boot. I have one listener public class ExecutorListener implements ServletContextListener .

How can I register this listener for Spring Boot? I've tried:

@SpringBootApplication
@ComponentScan
public class Application extends SpringBootServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new ExecutorListener());
    }

}

But the contextInitialized method is not called.

You can try couple of things: Register ExecutorListener as a @Bean explicitly:

@Bean
public ExecutorListener executorListener() {
   return new ExecutorListener();
}

or

You can try it with explicitly creating ServletRegistrationBean:

@Bean
public DispatcherServlet dispatcherServlet() {
    DispatcherServlet servlet=new DispatcherServlet();
    servlet.getServletContext().addListener(new ExecutorListener());
    return  servlet;
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(dispatcherServlet(), "/rest/v1/*");
    registrationBean
            .setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);


    return registrationBean;
}

If using an embedded container, there will soon be a third option if using SpringBoot 1.3.0+ Annotate your ServletContextListener implementation with @WebListener from servlet spec 3, then annotate one of your Spring @Configuration classes with the new @ServletComponentScan (and optionally tell it which packages to scan for filters, servlets and listeners).

Only available in 1.3.0+ at the moment though: http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/ServletComponentScan.html

Docs: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners

In case you prefer auto discovery using annotations only, make your ExecutorListener implement the ServletContextInitializer and eg annotate it with javax.annotation.ManagedBean . From there, just implement the onStartup method:

@ManagedBean
public final class ExecutorListener implements ServletContextInitializer {
  ...
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
      ...
    }
}

Another way is to create a class implements ServletContextListener and add @WebListner to the top which will tell Springboot this is a ServletContextListener , then add @ServletComponentScan(basePackages = "xxx") in SpringBootApplication to actually register it into the container

@WebListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ...
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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