☆Spring MVC + Mybatis + Spring Aop声明式事务管理没有作用

1 问题描述

在最近的一个项目中,采用springMVC、mybatis,发现一个很恼人的问题:事务管理不起作用!!网上查阅了大量的资料,尝试了各种解决办法,亦未能解决问题!

applicationContext.xml配置:

<context:annotation-config />
<!-- 引入jdbc属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />   
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean> 
<bean id="ftpConfig" class="com.wtas.utils.CustomizedPropertyConfig">   
    <property name="location" value="classpath:ftp.properties" />
</bean>

<!-- 配置mybatis用到的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatisConfig.xml" />
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="com.wtas.study.domain,com.wtas.studygroup.domain,com.wtas.sys.domain,com.wtas.test.domain" />
    <property name="mapperLocations" value="classpath*:com/wtas/**/*Mapper.xml" />
</bean>
<!-- 根据映射文件动态生成dao的实现 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.wtas" />
    <property name="annotationClass" value="org.springframework.stereotype.Repository" />
</bean>
<!-- 事务控制 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="userTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="remove*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
        <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
        <tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
        <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
        <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
        <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
        <tx:method name="publish*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
        <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="list*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="page*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
</tx:advice>
<tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- execution(public * com.wtas.*.service.*.*(..)) OR   execution(public * com.wtas.*.*.*.service.*.*(..))-->
<aop:config>
    <aop:pointcut id="pc" expression="execution(public * com.wtas.*.service.*.*(..))" />
    <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config>

springMVC.xml配置:

<!-- 这里注册转换器 -->
<mvc:annotation-driven conversion-service="conversionService" />
        <context:component-scan base-package="com.wtas"></context:component-scan>
<mvc:default-servlet-handler />

<!-- 注册ConversionService -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <!-- 转换器 -->
    <property name="converters">
        <list>
            <bean class="com.wtas.converter.StringToDateConverter">
                <constructor-arg value="yyyy-MM-dd" />
            </bean>
        </list>
    </property>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
                <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>

<!-- 视图解释类 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsps/" />
    <property name="suffix" value=".jsp" />
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

2 排查过程

具体原理,请参考:深入分析Spring与Spring MVC容器

3 解决问题

applicationContext.xml中添加:

<context:component-scan base-package="com.wtas">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
</context:component-scan>

springMVC.xml中修改:

<!-- 扫描所有的controller 但是不扫描service-->  
<context:component-scan base-package="com.wtas">  
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
</context:component-scan>  

4 总结问题

具体总结,可参见http://my.oschina.net/xianggao/blog/522267#OSC_h1_4

    原文作者:猿码道
    原文地址: https://www.jianshu.com/p/fdff748df792
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞