0

在将 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

这是我的 Spring 上下文配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  <bean class="org.permacode.atomic.web.AtomicStrutsWrapper"
    id="httpServletAccess" autowire="constructor"/>
  <bean class="org.permacode.atomic.web.DefaultExceptionHandler"
    id="exceptionHandler" autowire="constructor" />
  <bean class="org.permacode.atomic.web.ResourceBundleI18nEngine"
    id="i18nEngine">
    <constructor-arg>
      <bean class="org.permacode.atomic.web.StrutsResourceBundleProvider" />
    </constructor-arg>
  </bean>
  <bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory">
      <bean class="org.permacode.atomic.ContextManager"
        factory-method="getEntityManagerFactory" />
    </property>
  </bean>
  <bean id="protoDao" class="org.permacode.atomic.jpa.JpaProtoDao"></bean>
  <bean id="targetProxyProtoEM" class="org.permacode.atomic.domain.BasicProtoEntityManager">
    <constructor-arg ref="protoDao"/>
  </bean>
  <bean id="baseTransactionProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    abstract="true">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
      <props>
        <prop key="create*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
      </props>
    </property>
  </bean>
  <bean id="protoEntityManager" parent="baseTransactionProxy">
    <property name="target" ref="targetProxyProtoEM" />
  </bean>
</beans>

Spring 正在尝试将上下文中的 Spring bean 自动装配在一起,但在尝试创建事务管理器或事务代理管理器工厂时失败,目前尚不清楚。

我正在使用以下依赖项:

[INFO] The following files have been resolved:
[INFO]    antlr:antlr:jar:2.7.7:compile
[INFO]    aopalliance:aopalliance:jar:1.0:compile
[INFO]    cglib:cglib:jar:3.1:compile
[INFO]    cglib:cglib-nodep:jar:2.1_3:test
[INFO]    com.h2database:h2:jar:1.0.64:test
[INFO]    com.opensymphony:xwork:jar:2.1.2:compile
[INFO]    com.sun:tools:jar:1.5.0:system
[INFO]    commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO]    commons-collections:commons-collections:jar:3.2:compile
[INFO]    commons-configuration:commons-configuration:jar:1.5:compile
[INFO]    commons-dbutils:commons-dbutils:jar:1.1:compile
[INFO]    commons-digester:commons-digester:jar:1.8:compile
[INFO]    commons-fileupload:commons-fileupload:jar:1.2.1:compile
[INFO]    commons-io:commons-io:jar:1.3.1:compile
[INFO]    commons-lang:commons-lang:jar:2.4:compile
[INFO]    commons-logging:commons-logging:jar:1.1.1:compile
[INFO]    commons-logging:commons-logging-api:jar:1.1:compile
[INFO]    commons-pool:commons-pool:jar:1.5.4:compile
[INFO]    dom4j:dom4j:jar:1.6.1:compile
[INFO]    hsqldb:hsqldb:jar:1.8.0.7:test
[INFO]    httpunit:httpunit:jar:1.6.2:test
[INFO]    javax.servlet:jstl:jar:1.1.2:compile
[INFO]    javax.servlet:servlet-api:jar:2.5:provided
[INFO]    javax.servlet.jsp:jsp-api:jar:2.1:provided
[INFO]    javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO]    jtidy:jtidy:jar:4aug2000r7-dev:test
[INFO]    junit:junit:jar:4.8.2:test
[INFO]    mysql:mysql-connector-java:jar:5.0.5:test
[INFO]    net.sourceforge.serp:serp:jar:1.14.1:compile
[INFO]    opensymphony:ognl:jar:2.6.11:compile
[INFO]    org.apache.derby:derby:jar:10.3.2.1:test
[INFO]    org.apache.geronimo.specs:geronimo-jms_1.1_spec:jar:1.1.1:compile
[INFO]    org.apache.geronimo.specs:geronimo-jpa_2.0_spec:jar:1.1:compile
[INFO]    org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1:compile
[INFO]    org.apache.openjpa:openjpa:jar:2.3.0:compile
[INFO]    org.apache.struts:struts2-core:jar:2.1.6:compile
[INFO]    org.apache.struts:struts2-tiles-plugin:jar:2.1.6:compile
[INFO]    org.apache.tiles:tiles-api:jar:2.0.6:compile
[INFO]    org.apache.tiles:tiles-core:jar:2.0.6:compile
[INFO]    org.apache.tiles:tiles-jsp:jar:2.0.6:runtime
[INFO]    org.apache.tomcat:annotations-api:jar:6.0.13:test
[INFO]    org.apache.tomcat:catalina:jar:6.0.13:test
[INFO]    org.apache.tomcat:el-api:jar:6.0.13:test
[INFO]    org.apache.tomcat:jasper:jar:6.0.13:test
[INFO]    org.apache.tomcat:jasper-el:jar:6.0.13:test
[INFO]    org.apache.tomcat:jasper-jdt:jar:6.0.13:test
[INFO]    org.apache.tomcat:jsp-api:jar:6.0.13:test
[INFO]    org.apache.tomcat:juli:jar:6.0.13:test
[INFO]    org.apache.tomcat:servlet-api:jar:6.0.13:test
[INFO]    org.apache.xbean:xbean-asm4-shaded:jar:3.14:compile
[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.freemarker:freemarker:jar:2.3.13:compile
[INFO]    org.hibernate:hibernate-core:jar:4.2.11.Final:compile
[INFO]    org.hibernate:hibernate-entitymanager:jar:4.2.11.Final:compile
[INFO]    org.hibernate.common:hibernate-commons-annotations:jar:4.0.2.Final:compile
[INFO]    org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO]    org.javassist:javassist:jar:3.18.1-GA:compile
[INFO]    org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
[INFO]    org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.1.Final:compile
[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.springframework:spring-aop:jar:3.2.8.RELEASE:compile
[INFO]    org.springframework:spring-beans:jar:3.2.8.RELEASE:compile
[INFO]    org.springframework:spring-context:jar:3.2.8.RELEASE:compile
[INFO]    org.springframework:spring-core:jar:3.2.8.RELEASE:compile
[INFO]    org.springframework:spring-expression:jar:3.2.8.RELEASE:compile
[INFO]    org.springframework:spring-jdbc:jar:3.2.8.RELEASE:compile
[INFO]    org.springframework:spring-orm:jar:3.2.8.RELEASE:compile
[INFO]    org.springframework:spring-test:jar:2.5.6:compile
[INFO]    org.springframework:spring-tx:jar:3.2.8.RELEASE:compile
[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
[INFO]    p6spy:p6spy:jar:2.0.1:test
[INFO]    postgresql:postgresql:jar:8.2-507.jdbc3:test
[INFO]    taglibs:standard:jar:1.1.2:compile

org.permacode 项目中的所有类都相对简单,这里的整个测试旨在验证应用程序对 Spring 上下文和事务处理的实现和管理,因此该项目中的本地类如 AtomicConfigurationBean 和 ContextManager。除了包装 JPA 实体管理器工厂或 Spring 应用程序上下文之外,这些实际上并没有做任何危险的事情。

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

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

我不只是在寻找解决方案,我也会赞成或接受关于我应该从头开始解决这个问题的方法的好建议,假设任何顿悟都会迟到。

4

4 回答 4

1

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

[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.

于 2014-04-15T20:02:00.270 回答
0

我剥离了大多数依赖项并删除了大量代码,直到它工作为止,然后开始添加最新版本的依赖项,因为我取消了对代码的注释,它们成为编译和测试所必需的。

我在没有重新出现错误的情况下完成了这个过程。

这些是我的依赖列表中的差异:

$ 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

这是 maven 依赖项:列表输出,所以这些是我的所有依赖项以及已解决的依赖项。

所以 cglib.jar 出来了,而 cglib-nodep 是更高版本。

可疑的是,spring-test.jar 也被更新了,因为我现在明确依赖它,否则它会恢复到旧版本。然而,这本身并不会产生问题。

显然,现在返回并找出这些更改中的哪一个解决了问题显然不是我的首要任务,我想这可能是 @raphw 或 @jhadesdev 的答案之一或两者。

于 2014-05-02T13:42:43.577 回答
0

使用您对@jhadesdev 回答的评论中的附加信息:

您不应该您的类路径中删除cglib-nodep版本,而是删除仍然具有依赖关系的 cglib 版本。Cglib 在 ASM 之上运行。你对这个框架有明确的依赖(什么是坏的)。

正如ASM 的常见问题解答中所述,应该始终重新打包对该项目的依赖项,因为 API 在不同版本之间不一定兼容。您遇到的冲突很可能是因为

[INFO]    cglib:cglib:jar:3.1:compile
[INFO]    org.ow2.asm:asm:jar:4.2:compile

其中 cglib v3.1 依赖项需要后一个 ASM v4.2依赖项。您有可能在您的类路径上有另一个测试依赖项,该依赖项与另一个未重新打包的 ASM 版本一起使用。可能是一些模拟框架。您应该使用带有重新打包 ASM 的 cglib 版本,而不是使用此版本。

不过,我想知道你java.lang.NoClassDefFoundError说什么。我知道如果您从项目中完全删除 cglib,您会收到这样的错误,因为验证程序会抱怨某些类正在引用 cglib 而它不在类路径上。但是,如果您想要跟进,请仅使用无依赖版本运行您的代码并发布java.lang.NoClassDefFoundError.

于 2014-04-16T11:04:38.713 回答
0

有时这是现有编译类的问题,因为 eclipse 可以使用比 maven 不同的构建文件夹。

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

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

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

于 2014-04-16T11:09:59.117 回答