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()这个方法长不到,而配置文件中,再看看自己的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.properties</value>
            </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.xml</value>
        </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.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="connection.characterEncoding">UTF-8</prop>
                <prop key="connection.useUnicode">true</prop>
                <prop key="hibernate.connection.url">${jdbc.url}</prop>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <!--最大连接数 -->
                <prop key="hibernate.c3p0.max_size">50</prop>
                <!--连接超时时间 -->
                <prop key="hibernate.c3p0.timeout">120</prop>
                <!--statemnets缓存大小 -->
                <prop key="hibernate.c3p0.max_statements">100</prop>
                <!--每隔多少秒检测连接是否可正常使用 -->
                <prop key="hibernate.c3p0.idle_test_period">120</prop>
                <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3 -->
                <prop key="hibernate.c3p0.acquire_increment">2</prop>
                <!-- 每次都验证连接是否可用 -->
                <prop key="hibernate.c3p0.validate">true</prop>
            </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>GET</value>
                <value>HEAD</value>
                <value>POST</value>
            </list>
        </property>
    </bean>
</beans>

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<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.properties</value>
            </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.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="connection.characterEncoding">UTF-8</prop>
                <prop key="connection.useUnicode">true</prop>
                <prop key="hibernate.connection.url">${jdbc.url}</prop>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <!--最大连接数 -->
                <prop key="hibernate.c3p0.max_size">
                    50
                </prop>
                <!--连接超时时间 -->
                <prop key="hibernate.c3p0.timeout">
                    120
                </prop>
                <!--statemnets缓存大小 -->
                <prop key="hibernate.c3p0.max_statements">
                    100
                </prop>
                <!--每隔多少秒检测连接是否可正常使用 -->
                <prop key="hibernate.c3p0.idle_test_period">
                    120
                </prop>
                <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3 -->
                <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" />
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts" value="30" />
        <property name="breakAfterAcquireFailure" value="true" />
        <property name="testConnectionOnCheckout" value="false" />
    </bean>

    <!-- 配置Hibernate事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- 配置事务异常封装 -->
    <bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <!--  声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager -->
    <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(* ....*.*(..))" />
        <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
    </aop:config>

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



    <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.xml</value>
        </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>GET</value>
                <value>HEAD</value>
                <value>POST</value>
            </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);
   }
 }
java.lang.NoSuchMethodError: org.apache.commons.lang3.reflect.MethodUtils.ge。 这个错误通常发生在Java程序中,表示代码中调用的方法在所使用的库中不存在。具体来说,这个错误的原因是程序代码中使用了Apache Commons Lang库中的MethodUtils类的ge方法,但是实际的库中并没有这个方法。在这种情况下,Java虚拟机会抛出NoSuchMethodError异常,告知调用者该方法不存在。 要解决这个问题,我们可以考虑以下几个步骤: 1. 确认所使用的Apache Commons Lang库版本是否正确。可能是因为你使用的是一个过时的版本,其中并没有ge方法。可以通过查看官方文档或者Apache Commons Lang库的源代码来确认版本。 2. 检查代码中对MethodUtils.ge方法的调用。确认是否拼写错误、错误参数、错误的方法签名等导致方法找不到。 3. 如果确认版本和代码调用都正确,那可能是因为你使用的Apache Commons Lang版本与其他依赖库存在冲突。这种情况下,可能需要升级或者降级Apache Commons Lang库的版本,以解决冲突。 4. 如果以上步骤都没有解决问题,考虑尝试替换使用其他类似的方法,或者寻找其他解决方案来完成代码中原本需要使用MethodUtils.ge方法的操作。 总之,NoSuchMethodError异常是因为在程序代码中调用的方法在所使用的库中不存在。通过检查所使用的库版本、代码中的调用以及可能存在的依赖库冲突,可以解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值