Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register ServletContextListener in spring boot

Tags:

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.

like image 580
bilak Avatar asked Sep 04 '15 09:09

bilak


People also ask

Where do I put RequestContextListener in spring boot?

Just add a @Bean method which constructs the RequestContextListener . Spring Boot will do the rest.

What is the difference between ServletContextEvent and ServletContextListener?

ServletContextEvent class provides alerts/notifications for changes to a web application's servlet context. ServletContextListener is a class that receives alerts/notifications about changes to the servlet context and acts on them.

What is ServletContextListener in Java?

void contextInitialized(ServletContextEvent sce) Receives notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.

What is ServletContextListener when we use this?

ServletContextListener receives the notifications about changes to the servlet context and perform some action. ServletContextListener is used to perform important task at the time when context is initialized and destroyed.


2 Answers

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; } 
like image 184
jny Avatar answered Oct 11 '22 03:10

jny


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

like image 26
Tom Bunting Avatar answered Oct 11 '22 04:10

Tom Bunting