带有嵌入式tomcat 8.5的java Spring boot 2.2无法启动。disableRegistry不存在
在我的应用程序中,我们被迫使用tomcat 8.5,因为我们必须支持servlet api 3.1,但我们升级到了spring boot 2.2.6。现在,当开始使用嵌入式tomcat时,问题就出现了。如果我在我的pom中注释掉tomcat版本(获取tomcat 9),同样的代码就像一个符咒
这是我的EmbeddedTomcat代码:
@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
可能的相关问题:
am trying a Spring boot example but it is showing following error.. what should i do?
# 1 楼答案
Spring boot 2.2.6期望tomcat 9.0。x最小值。ref-https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/2.2.6.RELEASE
与spring 2.2.6并驾齐驱,带来tomcat 8.5。x、 也许您可以覆盖“spring boot starter tomcat”工件(来自spring boot父级:2.2.6),以实现兼容&;理想的tomcat版本
例如,弹簧靴启动器tomcat:2.0.7。这个版本可以很好地与tomcat 8.5配合使用。x
在pom文件中声明以下依赖项