最近的一次项目引入了MongoDB,撸完代码,启动服务,Junit单元测试一跑,报错:
java.lang.NoClassDefFoundError: org/springframework/core/DefaultParameterNameDiscoverer
at org.springframework.data.mapping.model.PreferredConstructorDiscoverer.<clinit>(PreferredConstructorDiscoverer.java:38)
at org.springframework.data.mapping.model.BasicPersistentEntity.<init>(BasicPersistentEntity.java:90)
at org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity.<init>(BasicMongoPersistentEntity.java:67)
at org.springframework.data.mongodb.core.mapping.MongoMappingContext.createPersistentEntity(MongoMappingContext.java:89)
at org.springframework.data.mongodb.core.mapping.MongoMappingContext.createPersistentEntity(MongoMappingContext.java:37)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:279)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:180)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:67)
at org.springframework.data.mongodb.core.MongoTemplate.assertUpdateableIdIfNotSet(MongoTemplate.java:1138)
at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:708)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:672)
at com.isz.app.baseDao.MongodbBaseDao.insert(MongodbBaseDao.java:250)
at com.isz.app.push.mapper.impl.PushMessageTaskMapperImpl.insertPushMessageTask(PushMessageTaskMapperImpl.java:64)
at com.isz.app.push.biz.PushServiceBiz.insertPushMessageTask(PushServiceBiz.java:136)
at com.isz.app.push.biz.PushServiceBiz.singlePush(PushServiceBiz.java:44)
at com.isz.app.push.service.impl.PushServiceFacade.singlePush(PushServiceFacade.java:22)
at com.isz.app.push.service.impl.PushServiceFacadeTest.test1(PushServiceFacadeTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.DefaultParameterNameDiscoverer
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 46 more
看到ClassNotFoundException(注意ClassNotFoundException和NoClassDefFoundError的区别),主要是因为org.springframework.core.DefaultParameterNameDiscoverer这个类找不到,下面开始分析过程:
1.先看了下项目中spring-core的jar包用的是3.2.4.release版本,打开源码,确实没有DefaultParameterNameDiscoverer这个类
2.判断是用于MongoDB中哪个Spring的jar包和我现在项目中Spring的jar包冲突了
3.debug一步步找到报错的地方(Spring代码的风格只要是以do开头都是具体的实现类),在new PreferredConstructorDiscoverer这个类的时候报错,打开这个类,根据报错信息,这个类的38行:
4.这里确实是用到了DefaultParameterNameDiscoverer,这个类所在的jar包是,spring-data-commons-1.9.3-RELEAS版本,说明1.9.3版本和3.2.4core是不兼容的, 1.9.3版本是要对应spring-core-4.0.0.0以上的(下载了4.0的core源码,是有DefaultParameterNameDiscoverer这个类的)
5.现在要确定的是这个项目是怎么引入spring-data-commons-1.9.3-RELEAS这个jar包的,这个时候就用到了maven的一个依赖树命令mvn:dependency:tree,这个命令可以查看到整个项目的所有依赖
这种属于一级依赖,去掉后,再查看项目依赖树,原来的1.9.3依赖已经去掉了,现在用的是1.8.2的jar包
6.启动项目,OK,完美运行。