Java 升级后,测试在Eclipse中通过,在Maven中失败,Spring抛出BeanCreationException/NoClassDefFoundError$proxy10

Java 升级后,测试在Eclipse中通过,在Maven中失败,Spring抛出BeanCreationException/NoClassDefFoundError$proxy10,java,spring,maven,cglib,spring-ioc,Java,Spring,Maven,Cglib,Spring Ioc,在将Spring/Hibernate升级到最新版本后,我在maven下运行测试时遇到了问题 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'protoEntityManager' defined in class path resource [test_config/ioc.xml]: Invocation of init method failed

在将Spring/Hibernate升级到最新版本后,我在maven下运行测试时遇到了问题

org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'protoEntityManager' defined in class path resource [test_config/ioc.xml]: 
    Invocation of init method failed; 
    nested exception is java.lang.NoClassDefFoundError: 
    Could not initialize class $Proxy10
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:610)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at org.permacode.atomic.AtomicConfigurationBean.getEntityManagerFactory(AtomicConfigurationBean.java:191)
at org.permacode.atomic.ContextManager.getEntityManagerFactory(ContextManager.java:168)
at org.permacode.atomic.ContextManagerTest.test2InstantiateEntityManagerFactory(ContextManagerTest.java:77)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class $Proxy10
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:588)
at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:119)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:111)
at org.springframework.aop.framework.AbstractSingletonProxyFactoryBean.afterPropertiesSet(AbstractSingletonProxyFactoryBean.java:174)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
... 45 more
org.permacode项目中的所有类都相对简单,这里的整个测试旨在验证应用程序对Spring上下文和事务处理的实现和管理,因此本项目中的本地类,如AtomicConfigurationBean和ContextManager。除了包装JPA实体管理器工厂或Spring应用程序上下文之外,它们实际上并没有做任何危险的事情

我有不止一个JPA提供者的依赖关系,因为项目允许在它们之间进行交换——这不是一个错误。我认为这不是问题的根源,因为我怀疑cglib或其他与代理有关的东西是这里的问题

昨天,我努力寻找解决方案,筛选了大量关于BeanCreationException和NoClassDefFoundErrors的求助信息,但通常受害者的类路径中缺少一些可靠的类。在这种情况下,尽管Spring抱怨$proxy10,但我认为它是通过IOC配置的一个或另一个bean的名称


我不仅仅是在寻找解决方案,我也会投票或接受好的建议作为答案,我应该采取什么方法从头开始解决这个问题,假设任何顿悟都会推迟到来

我认为问题可能与依赖关系的这一部分有关:树:

[INFO]    cglib:cglib:jar:3.1:compile
[INFO]    cglib:cglib-nodep:jar:2.1_3:test
在测试范围内(因此在运行测试时),类路径中有两个cglib版本,一个是最近的3.1版本,另一个是cglib nodep版本,这是一个没有依赖项的cglib版本


尝试升级测试范围内的cglib nodep,或者在不需要的情况下完全删除它,或者只保留cglib。无论哪种方式,在不同的JAR中具有相同类的多个版本都是导致NoClassDefFoundError的常见原因,您对@jhadesdev答案的评论中提供了更多信息:

您应该从类路径中删除cglib nodep版本,但删除仍具有依赖项的cglib版本。Cglib在ASM上运行。您对这个框架有一个明确的依赖性(不好的地方)

正如中所述,应该始终将依赖项重新打包到此项目,因为API不一定在不同版本之间兼容。您正在经历的冲突很可能是由于

[INFO]    cglib:cglib:jar:3.1:compile
[INFO]    org.ow2.asm:asm:jar:4.2:compile
其中cglibv3.1依赖项。您的类路径上可能存在另一个测试依赖项,该测试依赖项与另一个未重新打包的ASM版本一起工作。可能是一些模拟框架。您不应该使用此版本,而应该使用


然而,我想知道您的
java.lang.NoClassDefFoundError
是怎么说的。我理解,如果将cglib完全从项目中删除,就会出现这样的错误,因为验证器会抱怨某些类在引用cglib时不在类路径上。但是,如果您想跟进,请仅使用无依赖版本运行代码,并发布
java.lang.NoClassDefFoundError

的堆栈跟踪,有时这是现有编译类的问题,因为eclipse可以使用比maven更多的构建文件夹

因此,测试可以在编译类的不同版本上运行 上周我遇到了这些问题,我能够通过以下步骤解决这些问题:

  • Eclipse:项目-->清理-->选择项目-->确定
  • 马文:目标:干净

这解决了我的问题。不要忘了在清理之后重新编译所有项目。

我去掉了大部分依赖项,删除了大量代码,直到它起作用,然后开始添加最新版本的依赖项,因为我取消了对代码的注释,它们成为编译和测试所必需的

我完成了这个过程,没有出现错误

以下是我的依赖项列表中的差异:

$ diff.exe orig.txt new.txt
3,4c3
< [INFO]    cglib:cglib:jar:3.1:compile
< [INFO]    cglib:cglib-nodep:jar:2.1_3:test
---
> [INFO]    cglib:cglib-nodep:jar:2.2.2:test
14,15c13,14
< [INFO]    commons-io:commons-io:jar:1.3.1:compile
< [INFO]    commons-lang:commons-lang:jar:2.4:compile
---
> [INFO]    commons-io:commons-io:jar:1.3.2:compile
> [INFO]    commons-lang:commons-lang:jar:2.3:compile
47d45
< [INFO]    org.apache.tomcat:jsp-api:jar:6.0.13:test
49d46
< [INFO]    org.apache.tomcat:servlet-api:jar:6.0.13:test
51,54c48,49
< [INFO]    org.aspectj:aspectjrt:jar:1.7.4:compile
< [INFO]    org.easymock:easymock:jar:2.3:test
< [INFO]    org.easymock:easymockclassextension:jar:2.2.2:test
< [INFO]    org.eclipse.persistence:javax.persistence:jar:2.0.0:compile
---
> [INFO]    org.easymock:easymock:jar:3.2:test
> [INFO]    org.eclipse.persistence:eclipselink:jar:2.3.0:test
63,68c58,61
< [INFO]    org.ow2.asm:asm:jar:4.2:compile
< [INFO]    org.ow2.easybeans:easybeans-jpa-eclipselink:pom:1.2.1:compile
< [INFO]    org.permacode:permacode:jar:1.0.0:compile
< [INFO]    org.slf4j:jcl-over-slf4j:jar:1.7.6:runtime
< [INFO]    org.slf4j:slf4j-api:jar:1.7.6:compile
< [INFO]    org.slf4j:slf4j-simple:jar:1.6.1:compile
---
> [INFO]    org.objenesis:objenesis:jar:1.3:test
> [INFO]    org.ow2.easybeans:easybeans-jpa-eclipselink-dependency:pom:1.2.4:test
> [INFO]    org.permacode:atomic-test-jar:jar:1.0.1:test
> [INFO]    org.permacode:permacode:jar:1.0.1:compile
76c69
< [INFO]    org.springframework:spring-test:jar:2.5.6:compile
---
> [INFO]    org.springframework:spring-test:jar:3.2.8.RELEASE:test
78,79d70
< [INFO]    org.springframework.data:spring-data-commons:jar:1.7.1.RELEASE:compile
< [INFO]    org.springframework.data:spring-data-jpa:jar:1.5.1.RELEASE:compile
$diff.exe orig.txt new.txt
3,4c3
<[INFO]cglib:cglib:jar:3.1:编译
<[INFO]cglib:cglib nodep:jar:2.1\u 3:test
---
>[INFO]cglib:cglibnodep:jar:2.2.2:test
14,15c13,14
<[INFO]commons io:commons io:jar:1.3.1:编译
<[INFO]commons-lang:commons-lang:jar:2.4:compile
---
>[INFO]commons io:commons io:jar:1.3.2:编译
>[INFO]commons-lang:commons-lang:jar:2.3:compile
47d45
<[INFO]org.apache.tomcat:jspapi:jar:6.0.13:test
49d46
<[INFO]org.apache.tomcat:servlet api:jar:6.0.13:test
51,54c48,49
<[INFO]org.aspectj:aspectjrt:jar:1.7.4:compile
<[INFO]org.easymock:easymock:jar:2.3:test
<[INFO]org.easymock:easymockclassextension:jar:2.2.2:test
<[INFO]org.eclipse.persistence:javax.persistence:jar:2.0.0:compile
---
>[信息]org.easymock:easymock:jar:3.2:test
>[信息]org.eclipse.persistence:eclipselink:jar:2.3.0:test
63,68c58,61
<[INFO]org.ow2.asm:asm:jar:4.2:compile
<[INFO]org.ow2.easybeans:easybeans jpa eclipselink:pom:1.2.1:compile
<[INFO]org.permacode:permacode:jar:1.0.0:compile
<[INFO]org.slf4j:jcl-over-slf4j:jar:1.7.6:运行时
<[INFO]org.slf4j:slf4j api:jar:1.7.6:compile
<[INFO]org.slf4j:slf4j-simple:jar:1.6.1:compile
---
>[信息]org.objeness:objeness:jar:1.3:test
>[信息]org.ow2.easybeans:easybeans jpa eclipselink依赖关系:pom:1.2.4:test
>[信息]org.permacode:atomic test jar:jar:1.0.1:test
>[信息]org.permacode:permacode:jar:1.0.1:compile
76c69
<[INFO]org.springframework:spring测试:jar:2.5.6:compile
---
>[信息]org.springframework:spring-test:jar:3.2.8.RELEASE:test
78,79d70
<[INFO]org.springframework.data:spring数据共享:jar:1.7.1.RELEASE:compile
<[INFO]org.springframework.data:spring数据jpa:jar:1.5.1.RELEASE:compile
这是maven依赖项:列出输出,以便
[INFO]    cglib:cglib:jar:3.1:compile
[INFO]    org.ow2.asm:asm:jar:4.2:compile
$ diff.exe orig.txt new.txt
3,4c3
< [INFO]    cglib:cglib:jar:3.1:compile
< [INFO]    cglib:cglib-nodep:jar:2.1_3:test
---
> [INFO]    cglib:cglib-nodep:jar:2.2.2:test
14,15c13,14
< [INFO]    commons-io:commons-io:jar:1.3.1:compile
< [INFO]    commons-lang:commons-lang:jar:2.4:compile
---
> [INFO]    commons-io:commons-io:jar:1.3.2:compile
> [INFO]    commons-lang:commons-lang:jar:2.3:compile
47d45
< [INFO]    org.apache.tomcat:jsp-api:jar:6.0.13:test
49d46
< [INFO]    org.apache.tomcat:servlet-api:jar:6.0.13:test
51,54c48,49
< [INFO]    org.aspectj:aspectjrt:jar:1.7.4:compile
< [INFO]    org.easymock:easymock:jar:2.3:test
< [INFO]    org.easymock:easymockclassextension:jar:2.2.2:test
< [INFO]    org.eclipse.persistence:javax.persistence:jar:2.0.0:compile
---
> [INFO]    org.easymock:easymock:jar:3.2:test
> [INFO]    org.eclipse.persistence:eclipselink:jar:2.3.0:test
63,68c58,61
< [INFO]    org.ow2.asm:asm:jar:4.2:compile
< [INFO]    org.ow2.easybeans:easybeans-jpa-eclipselink:pom:1.2.1:compile
< [INFO]    org.permacode:permacode:jar:1.0.0:compile
< [INFO]    org.slf4j:jcl-over-slf4j:jar:1.7.6:runtime
< [INFO]    org.slf4j:slf4j-api:jar:1.7.6:compile
< [INFO]    org.slf4j:slf4j-simple:jar:1.6.1:compile
---
> [INFO]    org.objenesis:objenesis:jar:1.3:test
> [INFO]    org.ow2.easybeans:easybeans-jpa-eclipselink-dependency:pom:1.2.4:test
> [INFO]    org.permacode:atomic-test-jar:jar:1.0.1:test
> [INFO]    org.permacode:permacode:jar:1.0.1:compile
76c69
< [INFO]    org.springframework:spring-test:jar:2.5.6:compile
---
> [INFO]    org.springframework:spring-test:jar:3.2.8.RELEASE:test
78,79d70
< [INFO]    org.springframework.data:spring-data-commons:jar:1.7.1.RELEASE:compile
< [INFO]    org.springframework.data:spring-data-jpa:jar:1.5.1.RELEASE:compile