java - 带有嵌入式 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
可能的相关问题:
解决方案
这是一个非常丑陋的解决方案,可能不起作用,但你可以给它一个机会。
- 在您的应用程序包结构中创建,如 apache 的 lib /java/src/main/org/apache/tomcat/util/modeler/
- 将 Tomcat 8 中的 Registry 类放入此包中。
添加存根方法
disableRegistry
,例如public static synchronized void disableRegistry() { return new NoDescriptorRegistry(); }
推荐阅读
- javascript - 如何在节点 gyp 中包含两个 C++ 库 - Boost 和 Quantlib?
- javascript - 如何使用 CheerIo 获取第一个到第五个元素的标签数据
- javascript - 如何检查jsp中单击了哪个按钮?
- c# - C# 错误 - Guid 应包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
- python - 从文本文件中删除行号并将值提取到列表
- javascript - 获取数据后从变量中删除“<br><br>”
- node.js - Azure AD B2C 注册
- javascript - 限制 Ngrx 实体的大小
- flutter - 如何在 Dart Linter / Pull 请求中禁止 bang 运算符
- docker - 如何在 docker-stack 文件中使用 wait-for-it?