【问题标题】:Spring Boot upgrade can't add RequestContextListenerSpring Boot升级无法添加RequestContextListener
【发布时间】:2018-03-02 14:43:45
【问题描述】:

所以我刚刚将我的 Spring Boot Web 应用升级到 2.0.0,在我的主应用程序类中,我有这个方法:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        servletContext.addListener(new RequestContextListener());
    }
    else {
        logger.debug("No ContextLoaderListener registered");
    }   
}

但是现在我得到一个编译错误:

The method addListener(RequestContextListener) is undefined for the type ServletContext

奇怪的是 ServletContext 是问题所在,而不是 Spring boot。它不再有任何 add* 方法。 Spring5/Boot2 是否升级了 servlet 规范,以及 现在这样做的正确方法是什么?

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    这是一个 ServletContext 问题。 Spring boot 2 使用 servlet 规范 3.1.0,显然不再支持 2.5,这是我在我的 pom.xml 中使用的。所以我用这个替换它:

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency> 
    

    而且效果很好。希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 2021-06-16
      • 2021-01-18
      • 2022-06-24
      • 2021-01-18
      • 2022-07-19
      • 1970-01-01
      • 2022-12-22
      • 2018-10-31
      相关资源
      最近更新 更多