配置文件使用aop思想進(jìn)行配置
一挨摸、步驟
(1)配置事務(wù)管理器
(2)配置事務(wù)的增強(qiáng)
(3)配置切入點(diǎn)和切面
二孩革、代碼
Service層OrdersService.java
import org.springframework.beans.factory.annotation.Autowired;
import work.zhangdoudou.Dao.OrdersDao;
public class OrdersService {
@Autowired
private OrdersDao ordersDao;
/*
* 調(diào)用dao層
* 業(yè)務(wù)邏輯,寫轉(zhuǎn)賬業(yè)務(wù)
*/
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;
/*
* 對(duì)數(shù)據(jù)庫(kù)操作得运,不屑業(yè)務(wù)
*/
//張三少錢的方法
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>
<!-- 創(chuàng)建jdbcTemplate對(duì)象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- dataSource 傳遞到模板中 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 1膝蜈、 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事務(wù)的增強(qiáng) -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 做事務(wù)的操作 -->
<tx:attributes>
<!-- 設(shè)置進(jìn)行事務(wù)操作的方法匹配規(guī)則 設(shè)置隔離級(jí)別 -->
<tx:method name="account*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 3、配置切面 -->
<aop:config>
<!-- 切入點(diǎn) -->
<aop:pointcut expression="execution(* work.zhangdoudou.Service.OrdersService.*(..))" id="pointcut1"/>
<!-- 切面 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
測(cè)試類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);
}
}
三熔掺、正常情況下運(yùn)行結(jié)果
四饱搏、讓其出現(xiàn)異常的運(yùn)行結(jié)果
異常代碼
運(yùn)行結(jié)果