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 @   expworld  阅读(100)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示