Spring Boot升级无法添加RequestContextListener

问题描述 投票:0回答:1

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

@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启动。它不再有任何add *方法。 Spring5 / Boot2是否升级了servlet规范,现在这样做的正确方法是什么?

spring-boot
1个回答
0
投票

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

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

它工作得很好。希望这有助于某人。

© www.soinside.com 2019 - 2024. All rights reserved.