spring 声明事务管理(xml)
2018-06-08 本文已影响3人
DouDouZH
配置文件使用aop思想进行配置
一、步骤
(1)配置事务管理器
image.png
(2)配置事务的增强
image.png
(3)配置切入点和切面
image.png
二、代码
Service层OrdersService.java
import org.springframework.beans.factory.annotation.Autowired;
import work.zhangdoudou.Dao.OrdersDao;
public class OrdersService {
@Autowired
private OrdersDao ordersDao;
/*
* 调用dao层
* 业务逻辑,写转账业务
*/
public void accountMoney(String name1,String name2,Integer money) {
//张三少1000块钱
ordersDao.lessMoney(name1,money);
//李四多1000快钱
ordersDao.moreMoney(name2,money);
}
}
dao层OrdersDao.java
package work.zhangdoudou.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
public class OrdersDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/*
* 对数据库操作,不屑业务
*/
//张三少钱的方法
public void lessMoney(String name,Integer money) {
String sql="UPDATE account SET salary=salary-? WHERE u_name=?";
jdbcTemplate.update(sql,money,name);
}
//李四多钱的方法
public void moreMoney(String name,int money) {
String sql="UPDATE account SET salary=salary+? WHERE u_name=?";
jdbcTemplate.update(sql,money,name);
}
}
配置文件applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 开启器注解扫描 -->
<context:component-scan base-package="work.zhangdoudou"></context:component-scan>
<!-- value 从配置文件里面 db.properties中取值 -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" ></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="ordersDao" class="work.zhangdoudou.Dao.OrdersDao"></bean>
<bean id="ordersService" class="work.zhangdoudou.Service.OrdersService"></bean>
<!-- 创建jdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- dataSource 传递到模板中 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 1、 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 做事务的操作 -->
<tx:attributes>
<!-- 设置进行事务操作的方法匹配规则 设置隔离级别 -->
<tx:method name="account*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 3、配置切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* work.zhangdoudou.Service.OrdersService.*(..))" id="pointcut1"/>
<!-- 切面 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
测试类Test1.java
package work.zhangdoudou.Test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.Service.OrdersService;
public class Test1 {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
OrdersService ordersService=(OrdersService) context.getBean("ordersService");
ordersService.accountMoney("张三","李四",1000);
}
}
三、正常情况下运行结果
image.png四、让其出现异常的运行结果
异常代码
异常代码
运行结果
运行结果