spring学习(十)—事务管理(配置文件方式实现)
来源:互联网 发布:大乐网络注册 编辑:程序博客网 时间:2024/05/19 07:26
转载自传智博客学习视频
配置文件方式实现事务管理的步骤:
1.在配置文件中配置事务管理器
2.配置事务增强(增强的逻辑)
3.配置切面
(1)配置切入点(实际被增强的方法)
(2)配置切面,指定把哪个增强用到哪个切入点上。
1.代码组织结构
2.服务层类
package service;import dao.OrderDao;public class OrdersService { public OrderDao orderDao; public void setOrderDao(OrderDao orderDao) { this.orderDao = orderDao; } public void accountMoney(){ orderDao.lessMoney(); int i = 10/0; orderDao.moreMoney(); }}
3.数据库层类
package dao;import org.springframework.jdbc.core.JdbcTemplate;public class OrderDao { public JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void lessMoney() { String sql="update account set salary=salary+? where username=?"; jdbcTemplate.update(sql,-1000,"xiaowang"); } public void moreMoney() { String sql="update account set salary=salary+? where username=?"; jdbcTemplate.update(sql,1000,"xiaoma"); }}
4.测试类
package service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestService { @Test public void testService(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); OrdersService ordersService = (OrdersService) context.getBean("ordersService"); ordersService.accountMoney(); }}
5.配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 注入属性值 --> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///springlearn"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入dataSource --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务的增强,transactionManager是上面的事务管理器的id --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <!-- 做事务操作 --> <tx:attributes> <!-- 设置进行事务操作的方法匹配规则 --> <tx:method name="account*" propagation="REQUIRED"></tx:method> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config> <!-- 切入点 --> <aop:pointcut expression="execution(* service.OrdersService.*(..))" id="pointcut1"></aop:pointcut> <!-- 切面 --> <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"></aop:advisor> </aop:config> <bean id="ordersService" class="service.OrdersService"> <property name="orderDao" ref="orderDao"></property> </bean> <bean id="orderDao" class="dao.OrderDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!-- 创建jdbc模板对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean></beans>
0 0
- spring学习(十)—事务管理(配置文件方式实现)
- spring学习(十一)—事务管理(注解方式实现)
- spring的事务管理(配置文件方式和注解方式实现、转账环境搭建)
- 14-spring的事务管理(配置文件实现)
- spring学习笔记(7)——spring事务管理方式
- spring 事务管理实现方式
- Spring事务管理方式实现
- Spring学习笔记(16)----使用Spring配置文件实现事务管理
- Spring学习笔记(18)----使用Spring配置文件实现事务管理
- Spring学习笔记(18)----使用Spring配置文件实现事务管理
- spring事务管理—配置文件
- 使用Spring配置文件实现事务管理
- 使用Spring配置文件实现事务管理
- Spring学习笔记(四)Spring配置文件方式实现AOP
- spring学习(七)—AOP通过配置文件方式实现
- spring源码分析之——spring 事务管理实现方式
- spring mvc+hibernate 实现事务管理(配置文件版)
- spring mvc+hibernate 实现事务管理(配置文件版)
- python利用LSTM进行时间序列分析预测
- Sequence alignment
- Netty高性能之道
- Code Interview(1)
- Java - 集合
- spring学习(十)—事务管理(配置文件方式实现)
- 【Servlet】05-使用JSP
- TCP 的那些事儿(上)
- 安卓手势学习笔记
- Spring mvc+Mybatis整合
- POI获取Excel文件总行数和总列数
- 函数-控制流函数
- MySql-碰到问题记录
- C语言 输出格式总结