hsipeng / java_learnning_trip

spring springMVC mybatis
0 stars 1 forks source link

29.spring的声明式的事务处理 #33

Open hsipeng opened 7 years ago

hsipeng commented 7 years ago
hsipeng commented 7 years ago
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     "
    >

    <context:component-scan base-package="cn"/>
    <context:property-placeholder location="classpath:/jdbc.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${c3p0.driver}"></property>
    <property name="jdbcUrl" value="${c3p0.url}"></property>
    <property name="user" value="${c3p0.user}"></property>
    <property name="password" value="${c3p0.password}"></property>
    </bean>
    <!-- jdbc模板类 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <!-- jdbc模板类需要注入数据源 -->
       <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes id="" >
        <!-- 首字母add开头,进行匹配
            propagation="REQUIRED" 必须添加事务
            propagation="SUPPORTS" 事务支持的,有事务加事务,没有则不要

         -->
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut expression="execution(* cn.service..*(..))" id="pc"/>
        <aop:advisor advice-ref="txAdvice" pointcut="pc"/>
    </aop:config>
    </beans>