spring aop 事务不起作用

路路路路路 发布于 2017/06/13 09:40
阅读 364
收藏 0

配置信息:

<!-- 自动扫描 -->
    <context:component-scan base-package="com.msunsoft.cssd">
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>-->
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 第一种方式:加载一个properties文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>

    <!-- 配置oracle数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driverClasss}"/>
        <property name="url" value="${jdbcUrl}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>
    <!-- sqlserver数据源 -->
    <bean id="dataSourceSqlserver" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.sqlserver.drivername}" />
        <property name="url" value="${jdbc.sqlserver.url}" />
        <property name="username" value="${jdbc.sqlserver.username}" />
        <property name="password" value="${jdbc.sqlserver.password}" />

        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>
    <!-- 配置多数据源 -->
    <bean id="multipleDataSource" class="com.msunsoft.cssd.utils.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="dataSource"/>
        <property name="targetDataSources">
            <map>
                <!-- 注意这里的value是和上面的DataSource的id对应,key要和下面的CustomerContextHolder中的常量对应 -->
                <entry value-ref="dataSource" key="oracleDataSource"/>
                <entry value-ref="dataSourceSqlserver" key="sqlserverDataSource"/>
            </map>
        </property>
    </bean>

    <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="multipleDataSource"/>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath*:com/msunsoft/cssd/dao/**/support/*Mapper.xml"></property>
    </bean>
    <!-- 连接数据库session -->

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.msunsoft.cssd.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- 配置一个拦截器对象,处理具体的切换数据源的业务 -->
    <bean id="dataSourceMethodInterceptor" class="com.msunsoft.cssd.utils.DataSourceMethodInterceptor"/>
    <!-- 参与动态切换数据源的切入点对象 (切入点对象,确定何时何地调用拦截器) -->
    <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!-- 配置缓存aop切面 -->
        <property name="advice" ref="dataSourceMethodInterceptor" />
        <!-- 配置哪些方法参与缓存策略 -->
        <!--
            .表示符合任何单一字元
            ###  +表示符合前一个字元一次或多次
            ###  *表示符合前一个字元零次或多次
            ###  \Escape任何Regular expression使用到的符号
        -->
        <!-- .*表示前面的前缀(包括包名) 表示print方法-->
        <property name="patterns">
            <list>
                <value>com.msunsoft.cssd.service.*.support.*Service*\.*.*</value>
                <value>com.msunsoft.cssd.dao.*.support.*Mapper*\.*.*</value>
                <value>com.msunsoft.cssd.dao.*.*Dao*\.*.*</value>
            </list>
        </property>
    </bean>
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <!--<context:component-scan base-package="com.msunsoft.cssd.*">-->
        <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />-->
    <!--</context:component-scan>-->
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx-->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    <aop:aspectj-autoproxy />
    <!--  配置事务传播特性 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*"     propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="init*"    propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="save*"    propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="insert*"  propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="edit*"    propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="update*"  propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="modify*"  propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="delete*"  propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="remove*"  propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />
            <tx:method name="*hisCharge*"  propagation="REQUIRED" rollback-for="java.lang.Exception,java.lang.RuntimeException" />

            <tx:method name="find*"    propagation="SUPPORTS" read-only="true" />
            <tx:method name="query*"   propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*"        propagation="SUPPORTS" read-only="true" />
            <tx:method name="load*"    propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*"  propagation="SUPPORTS" read-only="true" />
            <!--<tx:method name="*"       propagation="REQUIRED" read-only="true"  rollback-for="java.lang.Exception,java.lang.RuntimeException" />-->
        </tx:attributes>
    </tx:advice>
    <!-- 配置参与事务的类-->
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.msunsoft.cssd.service..*.*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" order="1"/>
    </aop:config>

</beans>

 

 

 

<!-- 扫描指定包下的类 -->
    <context:component-scan base-package="com.msunsoft.cssd">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 -->
            </list>
        </property>
    </bean>
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <mvc:exclude-mapping path="/login/login.action" />
        <bean class="com.msunsoft.cssd.utils.SystemSessionInterceptor">
        </bean>
    </mvc:interceptor>
</mvc:interceptors>
<!-- 默认的注解映射的支持--> 
    <mvc:annotation-driven /> 
    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp"/>
    </bean>
    
     <!----><mvc:resources mapping="/resources/**" location="/resources/" />
    <!-- 配置哪些是静态资源,缺省Servlet直接返回 -->
    <!--<mvc:resources location="/static/**" mapping="/static/"/>
     <mvc:resources location="/resources/images/**" mapping="/resources/images/"/>
    <mvc:resources location="/resources/scripts/**" mapping="/resources/scripts/"/>
    <mvc:resources location="/resources/styles/**" mapping="/resources/styles/"/> -->
    <mvc:resources mapping="/WEB-INF/view/**" location="/WEB-INF/view/" />
   
    <!-- 文件上传配置 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="UTF-8"/>
        <!-- 上传文件大小限制为31M,31*1024*1024 -->
        <property name="maxUploadSize" value="32505856"/>
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="4096"/>
    </bean>
    <context:component-scan base-package="com.msunsoft.cssd.*" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>

</beans>

目录结构

service实现类:

updateSid失败后 insertRecoveryRecord插入成功 没有回滚,求大神指导

加载中
返回顶部
顶部