【发布时间】:2016-02-13 00:15:54
【问题描述】:
我使用 spring-boot 将 spring rest 服务部署为外部 tomcat 上的 war 文件。
引起:java.lang.NoSuchMethodError: org.springframework.util.StringUtils.isEmpty(Ljava/lang/Object;)Z
spring 版本由 maven 中的 spring-boot-starter-parent 管理,但我总是遇到不兼容的 spring 版本的典型异常。我测试:
spring-boot-starter-parent v*1.2.7.RELEASE* 和 v*1.3.0.BUILD-SNAPSHOT*
tomcat v8.0.3 和 8.0.28
奇怪的是我也部署为无错误的可运行 jar 文件。我也反编译了war文件,在\WEB-INF\lib\spring-core-4.1.8.RELEASE中找到了org.springframework.util.StringUtils.isEmpty。
我还检查了 maven 调试输出,发现只有一个具有正确版本 (4.1.8.RELEASE) 的 spring-core 工件。
可能tomcat内部也使用spring-core,有冲突吗?
Tomcat 日志文件中的异常部署:
Caused by: java.lang.NoSuchMethodError: org.springframework.util.StringUtils.isEmpty(Ljava/lang/Object;)Z
at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLogger(LogbackLoggingSystem.java:159)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:91)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartedEvent(LoggingApplicationListener.java:155)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:141)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100)
at org.springframework.boot.context.event.EventPublishingRunListener.started(EventPublishingRunListener.java:54)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:278)
at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:134)
at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:125)
at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:81)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5170)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 45 more
我的 pom.xml:
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.cherrypit</groupId>
<artifactId>lisa</artifactId>
<version>0.1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<packaging>war</packaging>
...
【问题讨论】:
-
考虑将
${spring.version} 添加到您的 Spring 依赖项中。 -
啊,是的!你是对的。它是一个弹簧类方法:)
-
@JustinKSU 如果我添加 spring.version 我会变成这个警告:
This expression evaluates to 4.1.8.RELEASE The property is defined in org.springframework.boot:spring-boot-dependencies:1.2.7.RELEASE -
@JustinKSU 我得到了与
${spring.version} 相同的异常 -
如果你得到一个 NoSuchMethod,很可能这个类已经在不同的版本中加载了。找出答案的最佳方法是检查依赖项,例如:
mvn dependency:tree > c:\dep.txt
标签: java spring tomcat spring-boot nosuchmethoderror