首页 > 解决方案 > 带有嵌入式 tomcat 8.5 的 Spring Boot 2.2 无法启动。disableRegistry 不存在

问题描述

在我的应用程序中,我们被迫使用 tomcat 8.5,因为我们必须支持 servlet api 3.1,但我们升级到 spring boot 2.2.6。现在开始使用嵌入式tomcat时问题正在发生。如果我在我的 pom 中注释掉 tomcat 版本(获取 Tomcat 9),那么相同的代码就像一个魅力。

这是我的 EmbededTomcat 代码:

@Value("${spring.datasource.url}")
private String jdbcUrl;

@Value("${spring.datasource.username}")
private String jdbcUsername;

@Value("${spring.datasource.password}")
private String jdbcPassword;

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;

@Bean
public TomcatServletWebServerFactory tomcatFactory() {
    log.info("initializing tomcat factory... ");        
    return new TomcatServletWebServerFactory() {

        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatWebServer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {    
            log.info("initializing tomcat factory JDNI ... ");
            // Adding connection details
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/DB");
            resource.setType(DataSource.class.getName());
            resource.setProperty("driverClassName", driverClassName);
            resource.setProperty("url", jdbcUrl);
            resource.setProperty("username", jdbcUsername);
            resource.setProperty("password", jdbcPassword);

            context.getNamingResources().addResource(resource);
        }
    };
}

在我的 pom.xml 上,我所做的唯一更改是:

<servlet-api.version>3.1.0</servlet-api.version> 
<!-- Forced for embeded tomcats since tomcat 9 force servlet 4.0 -->
<tomcat.version>8.5.54</tomcat.version>

这是完整的堆栈错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:175)

The following method did not exist:

    org.apache.tomcat.util.modeler.Registry.disableRegistry()V

The method's class, org.apache.tomcat.util.modeler.Registry, is available from the following locations:

    jar:file:/C:/Users/localAdministrator/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.54/tomcat-embed-core-8.5.54.jar!/org/apache/tomcat/util/modeler/Registry.class

It was loaded from the following location:

    file:/C:/Users/localAdministrator/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.54/tomcat-embed-core-8.5.54.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.apache.tomcat.util.modeler.Registry

可能的相关问题:

我正在尝试一个 Spring Boot 示例,但它显示以下错误.. 我该怎么办?

标签: javaspringspring-bootembedded-tomcat-8

解决方案


这是一个非常丑陋的解决方案,可能不起作用,但你可以给它一个机会。

  1. 在您的应用程序包结构中创建,如 apache 的 lib /java/src/main/org/apache/tomcat/util/modeler/
  2. 将 Tomcat 8 中的 Registry 类放入此包中。
  3. 添加存根方法disableRegistry,例如

    public static synchronized void disableRegistry() {
        return new NoDescriptorRegistry();
    }
    

推荐阅读