spring管理事物(增删改的事物回滚等)

applicationContext.xml 文件配置,可以查看事物这块

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.0.xsd"
    default-lazy-init="false">
    <description>Spring公共配置 </description>
    <context:component-scan base-package="com.cmcc.service,com.cmcc.dao,com.cmcc.exception" />
    <util:properties id="dataSourceProps" location="classpath:system.properties"/>
    <!--启用aop代理  -->
    <aop:aspectj-autoproxy/>
    <!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="#{dataSourceProps['jdbc.driver']}" />
        <property name="url" value="#{dataSourceProps['jdbc.url']}" />
        <property name="username" value="#{dataSourceProps['jdbc.username']}" />
        <property name="password" value="#{dataSourceProps['jdbc.password']}" />
        <property name="defaultAutoCommit" value="false" />
        <!-- Connection Pooling Info -->
        <property name="maxActive" value="#{dataSourceProps['dbcp.maxActive']}" />
        <property name="maxIdle" value="#{dataSourceProps['dbcp.maxIdle']}" />
    </bean>
   
    <!--事务管理开始 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    <aop:config>
        <aop:pointcut expression="execution(public * com.cmcc.service..*.*(..))" id="businessService" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" order="2"/>
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*"  propagation="REQUIRED"/>
            <tx:method name="save*"  propagation="REQUIRED"/>
            <tx:method name="del*"  propagation="REQUIRED"/>
            <tx:method name="put*" propagation="REQUIRED"/>
            <tx:method name="edit*" propagation="REQUIRED"/>
            <tx:method name="sort*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 事务管理结束 -->
    <!-- Spring iBatis SqlMapClient -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:com/cmcc/dao/mapper/**/*.xml"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.cmcc.dao"/>
       <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
   
    <bean id="springContextHolder" class="com.cmcc.util.SpringContextHolder" lazy-init="false" />
    <task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
    <task:executor id="myExecutor" pool-size="5"/>
    <task:scheduler id="myScheduler" pool-size="10"/>
   
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <!-- <property name="maxUploadSize" value="10485760" />  -->
    </bean>
</beans>

猜你喜欢

转载自wisfly.iteye.com/blog/2234318