如何添加没有XML配置的RequestContextListener? [英] How to add a RequestContextListener with no-xml configuration?

查看:132
本文介绍了如何添加没有XML配置的RequestContextListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

I use no-web.xml configuration, so I've got 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());
}

}

从上面的代码中可以看到,我已经在onStartup(ServletContext servletContext)方法中添加了一个侦听器,但是由于我仍然得到

As seen from code above, I have added a listener to onStartup(ServletContext servletContext) method, but it doesn't help as I still get

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

此消息.如何将侦听器正确添加到我的Spring Boot应用程序中?

this message. How can I properly add a listener to my Spring Boot Application?

推荐答案

我创建了此类,并解决了我的问题.

I created this class and that solved my issue.

@Configuration
@WebListener
public class MyRequestContextListener extends RequestContextListener {
}

这篇关于如何添加没有XML配置的RequestContextListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆