SSM10.3【Spring:基于注解的声明式事务控制】

 

 

 

 

代码+环境同10.2
以下仅说明不同的地方

 

复制代码
 1 package com.haifei.service;
 2 
 3 import com.haifei.dao.AccountDao;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Service;
 6 import org.springframework.transaction.annotation.Isolation;
 7 import org.springframework.transaction.annotation.Propagation;
 8 import org.springframework.transaction.annotation.Transactional;
 9 
10 /**
11  * 声明式事务控制明确事项:
12  * 谁是切点?   transfer()
13  * 谁是通知?   事务控制
14  * 配置切面?   AOP配置
15  */
16 @Service("accountService") //注解方式配置service的bean
17 @Transactional(isolation = Isolation.READ_COMMITTED)
18 public class AccountServiceImpl implements AccountService{
19 
20     @Autowired //注解方式依赖注入(set方法可以省略)
21     private AccountDao accountDao;
22     /*public void setAccountDao(AccountDao accountDao) {
23         this.accountDao = accountDao;
24     }*/
25 
26     //注解方式配置事务,注意虽然也可在直接在类上加事务(对类中所有方法生效),但实际方法上的事务遵从就近原则
27     @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
28     public void transfer(String outMan,String inMan,double money){
29         accountDao.out(outMan,money);
30         int i = 1/0; //java.lang.ArithmeticException: / by zero
31         accountDao.in(inMan,money);
32     }
33 
34 }
复制代码
复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="
 8             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 9             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
12 ">
13 
14     <!--加载外部的properties文件-->
15     <context:property-placeholder location="classpath:jdbc.properties"/>
16     <!--配置数据源(数据库连接池)-->
17     <bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
18         <property name="driverClass" value="${jdbc.driver}"></property>
19         <property name="jdbcUrl" value="${jdbc.url}"></property>
20         <property name="user" value="${jdbc.username}"></property>
21         <property name="password" value="${jdbc.password}"></property>
22     </bean>
23     <bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource">
24         <property name="driverClassName" value="${jdbc.driver}"></property>
25         <property name="url" value="${jdbc.url}"></property>
26         <property name="username" value="${jdbc.username}"></property>
27         <property name="password" value="${jdbc.password}"></property>
28     </bean>
29     <!--配置spring jdbcTemplate-->
30     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
31         <property name="dataSource" ref="dataSource_c3p0"/>  <!--数据源可选c3p0和druid-->
32     </bean>
33 
34 
35     <!--<bean id="accountDao" class="com.haifei.dao.AccountDaoImpl">
36         <property name="template" ref="jdbcTemplate"/>
37     </bean>
38     <bean id="accountService" class="com.haifei.service.AccountServiceImpl">
39         <property name="accountDao" ref="accountDao"/>
40     </bean>-->
41     
42     <!--采用注解方式配置bean及其依赖注入时,要记得开启组件扫描-->
43     <!--<context:component-scan base-package="com.haifei.dao" />
44     <context:component-scan base-package="com.haifei.service" />-->
45     <context:component-scan base-package="com.haifei" />
46 
47 
48     <!--配置平台事务管理器-->
49     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
50         <property name="dataSource" ref="dataSource_c3p0"/>
51     </bean>
52 
53     <!--采用注解方式配置事务时,要配置事务的注解驱动-->
54     <tx:annotation-driven transaction-manager="transactionManager"/>
55 
56     <!--&lt;!&ndash;配置通知-事务的增强&ndash;&gt;
57     <tx:advice id="txAdvice" transaction-manager="transactionManager">
58         <tx:attributes>
59             <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="1" read-only="false"/>
60         </tx:attributes>
61     </tx:advice>
62     &lt;!&ndash;配置事务的AOP织入&ndash;&gt;
63     <aop:config>
64         <aop:pointcut id="txPointcut" expression="execution(* com.haifei.service.*.*(..))" />
65         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
66     </aop:config>-->
67 
68 </beans>
复制代码

 

posted @   yub4by  阅读(62)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示