Spring4+Hibernate4整合经验:事务配置导致的java.lang.NoSuchMethodError异常

最近在做项目的时候,升级之前的项目框架,从spring3+hibernate3升级到spring4+hibernate4+shiro框架的时候,遇到了一个nosuchmethod的异常问题,网上大部分资料都是说jar冲突问题,但是jar使用mvn控制的,冲突都解决了,仍然报这个异常,并且提示的是applicationContext.xml上下文环境配置文件里的异常异常代码如下:

Caused by: java.lang.NoSuchMethodError: org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Ljava/lang/Object;)V
    at org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser.parseInternal(AnnotationDrivenBeanDefinitionParser.java:54) ~[spring-dao-2.0.8.jar:2.0.8]
    at org.springframework.beans.factory.xml.AbstractBeanDefinitionParser.parse(AbstractBeanDefinitionParser.java:60) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1427) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1417) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:174) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:144) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:100) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:510) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392) ~[spring-beans-4.1.3.RELEASE.jar:4.1.3.RELEASE]
    ... 56 common frames omitted

从异常上看出是registerproxycreatorifnecessary()这个方法长不到,而配置文件中,再看看自己的配置文件:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:jdbc.propertiesvalue>
            list>
        property>
    bean>

    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="fileEncoding" value="UTF-8" />
        <property name="location" value="classpath:config.properties" />
    bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>


    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>classpath:sqlConfig.xmlvalue>
        property>
        <property name="dataSource" ref="dataSource">
        property>
    bean>

    <bean id="sqlMapClientTemplate"  class="org.springframework.orm.ibatis.SqlMapClientTemplate">
        <property name="sqlMapClient" ref="sqlMapClient"/>
    bean>


    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    bean>



    <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" ><value>${jdbc.driverClassName}value>property>
        <property name="jdbcUrl" ><value>${jdbc.url}value>property>
        <property name="user" ><value>${jdbc.username}value>property>
        <property name="password" ><value>${jdbc.password}value>property>
        <property name="minPoolSize" value="10" />
        <property name="maxPoolSize" value="100" />
        <property name="maxIdleTime" value="1800" />
        <property name="acquireIncrement" value="3" />
        <property name="maxStatements" value="1000" />
        <property name="initialPoolSize" value="10" />
        <property name="idleConnectionTestPeriod" value="60" />
        <property name="acquireRetryAttempts" value="30" />
        <property name="breakAfterAcquireFailure" value="true" />
        <property name="testConnectionOnCheckout" value="false" />
    bean>
    <bean id = "sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                ...
            list>
        property>
        <property name="mappingResources" >
            <list>
                ...
            list>
        property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectprop>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.hbm2ddl.auto">updateprop>
                <prop key="connection.characterEncoding">UTF-8prop>
                <prop key="connection.useUnicode">trueprop>
                <prop key="hibernate.connection.url">${jdbc.url}prop>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driverprop>
                <prop key="hibernate.c3p0.min_size">5prop>
                
                <prop key="hibernate.c3p0.max_size">50prop>
                
                <prop key="hibernate.c3p0.timeout">120prop>
                
                <prop key="hibernate.c3p0.max_statements">100prop>
                
                <prop key="hibernate.c3p0.idle_test_period">120prop>
                
                <prop key="hibernate.c3p0.acquire_increment">2prop>
                
                <prop key="hibernate.c3p0.validate">trueprop>
            props>
        property>
    bean>

    
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/server/**" value="myResourceHandler" />
            map>
        property>
        <property name="order" value="100000" />
    bean>

    <bean id="myResourceHandler" name="myResourceHandler" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
        <property name="locations" value="/server/portal" />
        <property name="supportedMethods">
            <list>
                <value>GETvalue>
                <value>HEADvalue>
                <value>POSTvalue>
            list>
        property>
    bean>
beans>

配置文件比较乱,是早起配置的一个spring+hibernate+ibatis的框架,在上下文环境中,看到涉及到proxy的配置项是:

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

因为找不到其他的解决办法,那么就关于此处的配置升级到spring4.0的配置,看是否能解决此问题,以下是修改后的配置文件(略作整理):


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
       default-lazy-init="true">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:jdbc.propertiesvalue>
            list>
        property>
    bean>

    <bean id = "sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                ...
            list>
        property>
        <property name="mappingResources">
            <list>
                ...
            list>
        property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectprop>
                <prop key="hibernate.show_sql">falseprop>
                <prop key="hibernate.hbm2ddl.auto">updateprop>
                <prop key="connection.characterEncoding">UTF-8prop>
                <prop key="connection.useUnicode">trueprop>
                <prop key="hibernate.connection.url">${jdbc.url}prop>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driverprop>
                <prop key="hibernate.c3p0.min_size">5prop>
                
                <prop key="hibernate.c3p0.max_size">
                    50
                prop>
                
                <prop key="hibernate.c3p0.timeout">
                    120
                prop>
                
                <prop key="hibernate.c3p0.max_statements">
                    100
                prop>
                
                <prop key="hibernate.c3p0.idle_test_period">
                    120
                prop>
                
                <prop key="hibernate.c3p0.acquire_increment">
                    2
                prop>
                
                <prop key="hibernate.c3p0.validate">
                    true
                prop>
            props>
        property>
    bean>

    <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" ><value>${jdbc.driverClassName}value>property>
        <property name="jdbcUrl" ><value>${jdbc.url}value>property>
        <property name="user" ><value>${jdbc.username}value>property>
        <property name="password" ><value>${jdbc.password}value>property>
        <property name="minPoolSize" value="10" />
        <property name="maxPoolSize" value="100" />
        <property name="maxIdleTime" value="1800" />
        <property name="acquireIncrement" value="3" />
        <property name="maxStatements" value="1000" />
        <property name="initialPoolSize" value="10" />
        <property name="idleConnectionTestPeriod" value="60" />
        
        <property name="acquireRetryAttempts" value="30" />
        <property name="breakAfterAcquireFailure" value="true" />
        <property name="testConnectionOnCheckout" value="false" />
    bean>

    
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    bean>

    
    <bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        tx:attributes>
    tx:advice>

    <aop:config expose-proxy="true">
        
        <aop:pointcut id="txPointcut" expression="execution(* ....*.*(..))" />
        
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
    aop:config>

    



    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="fileEncoding" value="UTF-8" />
        <property name="location" value="classpath:config.properties" />
    bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>


    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>classpath:sqlConfig.xmlvalue>
        property>
        <property name="dataSource" ref="dataSource">
        property>
    bean>

    <bean id="sqlMapClientTemplate"  class="org.springframework.orm.ibatis.SqlMapClientTemplate">
        <property name="sqlMapClient" ref="sqlMapClient"/>
    bean>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/server/**" value="myResourceHandler" />
            map>
        property>
        <property name="order" value="100000" />
    bean>


    <bean id="myResourceHandler" name="myResourceHandler"
          class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
        <property name="locations" value="/server/portal" />
        <property name="supportedMethods">
            <list>
                <value>GETvalue>
                <value>HEADvalue>
                <value>POSTvalue>
            list>
        property>
    bean>

beans>

新的配置文件中,对事务的管理部分,使用了tx,aop来管理,并且取消了annotation方式的事务管理,问题得到解决,而具体的原因还未证明,关于annotation注解:
@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。
Spring团队的建议是你在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。

 @Service
 @Transactional(rollbackFor=Exception.class)   //对当前类的所有方法起作用
 @SuppressWarnings("serial")
 public class ButtonBo extends GlobalBo {
  ....
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) //具体方法上
    public Button findButton(String buttonid) throws BaseException {
     return hibernateEntityDao.get(Button.class, buttonid);
   }
 }

你可能感兴趣的:(springmvc)