5-02.Spring-基于注解的事务讲解

 

<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p ="http://www.springframework.org/schema/p"
       xmlns:context ="http://www.springframework.org/schema/context"
       xmlns:aop ="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 读取db.properties数据-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

    <!-- 配置c3p0数据源
    注:dbcp和c3po的 数据库连接的参数的属性名是不一样
    please attention。。。。。
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"/>
        <property name="jdbcUrl" value="${jdbcUrl}"/>
        <property name="user" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>


    <!-- 配置dao-->
    <bean id="accountDao" class="com.gyf.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

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



    <!--配置service-->
    <bean id="accountService" class="com.gyf.service.impl.AccountServiceImpl2">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置工厂代理-->
    <bean id="proxyService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!--接口-->
        <property name="proxyInterfaces" value="com.gyf.service.IAccountService"></property>
        <!--目标对象-->
        <property name="target" ref="accountService"></property>
        <!--切面对象:Spring做,就不用写-->

        <!-- 事务管理器-->
        <property name="transactionManager" ref="txManager"/>

        <!--transactionAttributes:事务属性/详情配置
            key:写方法名
            value写 事务配置
            格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
          传播行为         隔离级别          是否只读    异常回滚 异常提交
        -->
        <property name="transactionAttributes">
            <props>
                <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop>
                <prop key="add">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
                <prop key="delete">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
                <prop key="update">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
                <prop key="find">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop>
            </props>
        </property>
    </bean>
</beans>

 

 

 

 

 

 

posted @ 2019-04-28 23:17  expworld  阅读(106)  评论(0)    收藏  举报