1. 步骤一:恢复转账开发环境(转账开发环境见“
https://www.cnblogs.com/wyhluckdog/p/10137283.html”)
2.
步骤二:引入AOP的开发包
3.步骤三:引入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:jdbc="http://www.springframework.org/schema/jdbc"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>
* 管理C3P0连接池
* 先引入C3P0的jar包
* com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar * 编写配置文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
4.步骤四:创建对应的包结构和类(具体内容见
“
https://www.cnblogs.com/wyhluckdog/p/10137283.html”
)
* com.huida.demo1
* AccountService
* AccountServlceImpl
* AccountDao
* AccountDaoImpl
5.步骤五:引入Spring的配置文件,将类配置到Spring中
<bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
6.
步骤六:配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
7.
步骤七:配置事务增强
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
name :绑定事务的方法名,可以使用通配符,可以配置多个。
propagation :传播行为
isolation :隔离级别
read-only :是否只读
timeout :超时信息
rollback-for:发生哪些异常回滚.
no-rollback-for:发生哪些异常不回滚.
-->
<!-- 哪些方法加事务 -->
<tx:method name="pay" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
8.步骤八:书写切面类MyAdvice:
package com.huida.demo1; public class MyAdvice { public void log(){
System.out.println("添加日志");
} }
9.步骤九:配置AOP的切面
<bean id="myAdvice" class="com.huida.demo1.MyAdvice"></bean>
<aop:config>
<aop:aspect ref="myAdvice">
<aop:before method="log" pointcut="execution(* com.huida.demo1.AccountServiceImpl.pay(..))"/>
</aop:aspect>
</aop:config>
10.完整的配置文件的配置信息为:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="pay" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <bean id="myAdvice" class="com.huida.demo1.MyAdvice"></bean> <aop:config>
<aop:aspect ref="myAdvice">
<aop:before method="log" pointcut="execution(* com.huida.demo1.AccountServiceImpl.pay(..))"/>
</aop:aspect>
</aop:config> <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
<!-- <property name="transactionTemplate" ref="transactionTemplate"/> -->
</bean> </beans>
11.步骤十:编写测试类
package com.huida.demo1; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 { @Resource(name="accountService")
private AccountService accountService; @Test
public void run1(){
accountService.pay("小明","小红",1000);
}
}
12.单元测试run1方法,刷新spring-day03数据库中的user表,可以看到小明的money减少了1000,而小红的money增加了1000.