spring学习(十)—事务管理(配置文件方式实现)
2017-02-20 16:54
736 查看
转载自传智博客学习视频
配置文件方式实现事务管理的步骤:
1.在配置文件中配置事务管理器
2.配置事务增强(增强的逻辑)
3.配置切面
(1)配置切入点(实际被增强的方法)
(2)配置切面,指定把哪个增强用到哪个切入点上。
1.代码组织结构
2.服务层类
3.数据库层类
4.测试类
5.配置文件
配置文件方式实现事务管理的步骤:
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>
相关文章推荐
- spring的事务管理(配置文件方式和注解方式实现、转账环境搭建)
- Spring学习笔记(18)----使用Spring配置文件实现事务管理
- 14-spring的事务管理(配置文件实现)
- 使用Spring配置文件实现事务管理
- Spring学习笔记(16)----使用Spring配置文件实现事务管理
- 事务有哪些特性?spring的事务管理有几种方式实现,如何实现?spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
- Spring学习---(4)XML方式实现Spring声明式事务管理
- 使用Spring配置文件实现事务管理
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
- spring框架学习(八)spring管理事务方式之注解配置
- spring框架的学习(二)——Spring的bean管理(xml配置文件方式)
- SSH与SSM学习之Spring22——Spring事务之xml配置方式管理事务
- spring框架学习(七)spring管理事务方式之xml配置
- Spring学习---(5)注解方式实现Spring声明式事务管理
- spring学习(七)—AOP通过配置文件方式实现
- SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
- spring学习(十一)—事务管理(注解方式实现)
- (转)使用Spring配置文件实现事务管理
- Spring入门之事务管理【一】XML配置文件方式
- Spring学习笔记(18)----使用Spring配置文件实现事务管理