How to add RequestContextListener without xml configuration?

I need to add a listener to my Spring download application, in web.xml it looks like

<listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> 

I am using the no-web.xml configuration, so I have a class like

 public class AppFilterConfig extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Filter[] getServletFilters() { CharacterEncodingFilter filter = new CharacterEncodingFilter(); filter.setEncoding("UTF8"); filter.setForceEncoding(true); Filter[] filters = new Filter[1]; filters[0] = filter; return filters; } private int maxUploadSizeInMb = 5 * 1024 * 1024; // 5 MB @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } @Override protected void registerDispatcherServlet(ServletContext servletContext) { super.registerDispatcherServlet(servletContext); servletContext.addListener(new HttpSessionEventPublisher()); } @Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); servletContext.addListener(new RequestContextListener()); } } 

As you can see from the above code, I added a listener to the onStartup method (ServletContext servletContext), but this does not help, since I still get

 In this case, use RequestContextListener or RequestContextFilter to expose the current request. 

this post. How can I correctly add a listener to my Spring boot application?

+5
source share
2 answers

I created this class and solved my problem.

 @Configuration @WebListener public class MyRequestContextListener extends RequestContextListener { } 
+6
source

Write your own listener class that extends from RequestContextListener and register it using annotation. Something like that:

 @WebListener public class MyRequestContextListener extends RequestContextListener { } 
+1
source

All Articles