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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?