在我的应用程序中,我们被迫使用 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
可能的相关问题: