问题:
Spring boot应用突然出现启动不了,报错:
java.lang.NoSuchMethodError: org.springframework.web.context.support.ServletContextAwareProcessor: method <init>()V not found
at org.springframework.boot.context.embedded.WebApplicationContextServletContextAwareProcessor.<init>(WebApplicationContextServletContextAwareProcessor.java:40)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.postProcessBeanFactory(EmbeddedWebApplicationContext.java:114)
at org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext.postProcessBeanFactory(AnnotationConfigEmbeddedWebApplicationContext.java:181)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:521)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.nariit.JxxApplication.main(JxdApplication.java:25)
而之前都运行的好好的。中间依赖应用包有重新编译,但编译正常。
分析:
经人提醒,发现项目依赖spring包冲突。在Eclipse中通过Ctrl + Shift + T,查找“ServletContextAwareProcessor”类,找到spring-1.2.6.jar 与spring-web-4.3.6-RELEASE.jar 2个包中都含有该类,显然spring-web-4.3.6-RELEASE.jar才是我们真正想要的,那就要想办法排除掉不想要的spring.jar包。
解决:
因为spring-1.2.6.jar 与spring-web-4.3.6-RELEASE.jar均含有ServletContextAwareProcessor类,可以推知应用的依赖包中直接或间接引用了该spring-1.2.6。那如何找到引用了“spring-1.2.6.jar”的jar包呢?
正确的做法是使用Eclipse pom编辑器的"dependency hierarchy"功能,从右侧列表中点“spring:1.2.6”,在左侧引用树中找到引用它的jar包,假定找到引用包名为“bbb-framework:1.2.2”。接着,在当前应用的pom.xml文件中,找到bbb-framework依赖,排除掉其引用的spring包。
在pom.xml中使用exclusion排除配置方式:
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
加上排除代码后效果如图:
<dependency>
<groupId>com.a.bbb</groupId>
<artifactId>bbb-framework</artifactId>
<version>1.2.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
然后保存pom.xml,重新使用maven编译、运行,问题解决。