狗血一样的剧情,丢人事迹写成博客,检讨自己,提示后人。
剧情是这样:
公司有一套自己的parent POM,用以统一各系统的依赖,所以就不方便采用Spring Boot的继承方式了,也就是如下的方式:
4.0.0 org.springframework.boot spring-boot-dependencies 1.3.8.RELEASE com.aaa.bbb aaa-bbb jar 1.0.0-SNAPSHOT aaa-bbb org.springframework.boot spring-boot-starter-web
根据实际项目需要,得将POM文件改为如下依赖的写法:
4.0.0 aaa-bbb aaa-bbb-parent 2.0-SNAPSHOT com.aaa.bbb aaa-bbb jar 1.0.0-SNAPSHOT aaa-bbb org.springframework.boot spring-boot-dependencies 1.3.8.RELEASE pom import org.springframework.boot spring-boot-starter-web
为了方便解释问题,也把这个DEMO中唯一的Java类贴出来(Java类中没错误):
package com.aaa.bbb;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@EnableAutoConfigurationpublic class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); }}
但是实际写的过程中,一直出现如下的这个错误:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.logging.ClasspathLoggingApplicationListener at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:413) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:393) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:384) at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:262) at org.springframework.boot.SpringApplication.(SpringApplication.java:236) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) at com.turing.log2.SampleController.main(SampleController.java:20)Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at org.springframework.util.ClassUtils.forName(ClassUtils.java:249) at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:406) ... 7 moreCaused by: java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 21 more
查了半天,才突然意识到问题,于是我对比了一下spring-boot的对spring依赖的版本号:
Spring Boot 1.3.8.RELEASE 对应是 Spring 4.2.8.RELEASE,
而我公司的parent中,spring版本统一为4.1.7.RELEASE。
和JDK版本,DEMO搭建等等均没关系,这些时间花费的太冤枉了……小黑屋见……