小编典典

如何添加没有XML配置的RequestContextListener?

spring-boot

我需要在Spring Boot应用程序中添加一个侦听器,在web.xml中,它看起来像

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

我使用no-web.xml配置,所以我有一个像

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

}

从上面的代码可以看出,我向onStartup(ServletContext servletContext)方法添加了一个侦听器,但是它仍然无济于事

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

这条信息。如何正确地在Spring Boot应用程序中添加侦听器?


阅读 509

收藏
2020-05-30

共1个答案

小编典典

我创建了此类,这解决了我的问题。

@Configuration
@WebListener
public class MyRequestContextListener extends RequestContextListener {
}
2020-05-30