错误:java.lang.NoSuchMethodError:org.objectweb.asm.ClassWriter。<init>(I)V

我正在开发一个小的Spring应用程序。 我必须将学生信息的详细信息存储在数据库中。 我开发了一个SimpleFormController。 我使用了NetBeans + Hibernate mapping + Spring。 当我部署项目时,会发生以下错误。

我的spring-config-db-applicationContext.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- Hibernate session factory --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <constructor-arg index="0"> <value>${driverClassName}</value> </constructor-arg> <constructor-arg index="1"> <value>${url}</value> </constructor-arg> <constructor-arg index="2"> <value>${username}</value> </constructor-arg> <constructor-arg index="3"> <value>${password}</value> </constructor-arg> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <!-- <property name="configLocation"> <value>WEB-INF/classes/hibernate.cfg.xml</value> </property> --> <property name="mappingResources" > <list> <value>hibernate.cfg.xml</value> </list> </property> <!-- <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${dialect}</prop> <prop key="hibernate.show_sql">true</prop> <!--<prop key="hibernate.hbm2ddl.auto">create</prop>--> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> </beans> 

发生以下错误:

 ERROR (org.springframework.web.context.ContextLoader:213) - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [jndi:/localhost/Student/WEB-INF/classes/config/spring-db-applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1395) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:526) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:730) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:387) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:270) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:627) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:511) at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1231) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761) at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1471) at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:824) at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:350) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:196) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:619) Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47) at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30) at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24) at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104) at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:117) at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43) at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162) at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:135) at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55) at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56) at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:302) at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434) at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108) at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:61) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:238) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1304) at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:813) at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:731) at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1454) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1392) ... 48 more Mar 12, 2010 5:32:28 PM org.apache.catalina.core.StandardContext start SEVERE: Error listenerStart 

Hibernate所需的ASM版本( asm-1.5.3.jar )与Spring所要求的版本不兼容。 但是,实际上,我不知道为什么你的类path上有asm-2.2.3.jar(ASM捆绑在spring.jar和spring-core.jar中以避免AFAIK的问题)。 见HHH-2222 。

我在这里find了解决这个问题的办法: http : //www.hildeberto.com/2008/05/hibernate-and-jersey-conflict-on.html

NoSuchMethodError javadoc这样说:

如果应用程序尝试调用某个类的指定方法(静态或实例),并且该类不再具有该方法的定义,则抛出该方法。

通常,这个错误是由编译器捕获的; 这个错误只能在运行时发生,如果一个类的定义不兼容改变。

在你的情况下,这个错误是一个强烈的迹象表明 ,你的web应用程序正在使用定义org.objectweb.asm.*类的JAR的错误版本。

要解决这种问题,你应该在你的依赖POM中添加两个jar(如果使用Maven的话)

 <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> 

我在启动时使用一些不同的库版本初始化Spring时出现了同样的错误,但是当我在classpath(cp中的其他库不重要)中获得版本时,

  1. ASM-3.1.jar
  2. CGLIB的节点p-2.1_3.jar
  3. ASM-ATTRS-1.5.3.jar

我改变我的API为* cglib — —> cglib-nodep-2.2.jar * cglib-asm —到—> cglib-asm.jar(即最新的一个)

更新你的pom.xml

 <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.1</version> </dependency> 

cglib的不同版本之间有很多不兼容的地方,很多不好的build议可以在这个主题上使用(使用古老版本的cglib-nodeb等)。 在用古老版本的Spring(2.5.6)摔跤3天后,我发现:

  • 如果Spring从cglib中缺lesssetInterceptDuringConstruction(boolean)引发基于增强器的NoSuchMethodErrorexception,请检查maven中的依赖关系(依赖层次结构选项卡应列出您的项目受到污染的所有cglib版本)。 你可能有一个依赖,它引入了一个2.2之前版本的cglib,Spring Cglib2AopProxy将抓取Enhancer来执行代理。 将2.2+版本添加到您的依赖项中,但确保为这些其他版本添加排除项。 如果你不这样做,它会继续炸毁。
  • 确保在当前项目的maven依赖项中有足够高的cglib版本, 要小心挑选太高的版本,因为那样你会得到IncompatibleClassChangeError(s)。
  • 通过对有问题的项目进行maven更新,然后查看Project>首选项> Java构buildpath>库> Maven依赖关系,validation您的pom更改是否正确进入了项目。 如果你看到错误的cglib版本,再次编辑pom。

添加http-builder到依赖项时遇到同样的错误。
就我而言,我可以通过简单地排除asm来解决这个问题:

 compile('org.codehaus.groovy.modules.http-builder:http-builder:0.7'){ excludes 'xml-apis' exclude(group:'xerces', module: 'xercesImpl') excludes 'asm' }