目录
- 01,JdbcTemplate
- 02,spring中的事务管理(transactionManager)
- 2.1,Spring 事务控制
- 2.2,Spring 中事务控制的 API 介绍
- 2.3,spring的事务控制配置(XML)
- 03,案例演示
01,JdbcTemplate
- 先看一下这张图
- JDBC是Sun公司推出的连接数据库的规范(一些接口和抽象类),这样就不必要为不同的数据库编写驱动了,使我们使用数据库方便一点,但是直接使用JDBC连接数据库还是要很多步骤的。
- spring中的JdbcTemplate对JDBC又进行了简单的封装,使我们连接数据库又简单一些
1.1,JdbcTemplate 概述
- 它是 spring 框架中提供的一个对象,是对原始 JDBC API 对象的简单封装。 spring 框架为我们提供了很多的操作模板类。
- 操作关系型数据的:
JdbcTemplate(常用)
HibernateTemplate(不常用) - 操作 nosql (非关系型)数据库的:
RedisTemplate - 操作消息队列的:
JmsTemplate - 我们在使用spring的JdbcTemplate的时候需要导入相应的jar包
spring-jdbc-5.0.2.RELEASE.jar
- 一般我们还需要再导入一个jar包
spring-tx-5.0.2.RELEASE.jar
,它是和事务相关的jar包
1.2,JdbcTemplate 对象如何创建
- 通过查看JdbcTemplate的源码可知,除了默认构造函数之外,其他的构造函数都需要提供一个数据源。既然有set方法,我们就可以使用依赖注入,首先我们要在配置文件中配置这些对象
public JdbcTemplate() {
}
public JdbcTemplate(DataSource dataSource) {
setDataSource(dataSource);
afterPropertiesSet();
}
public JdbcTemplate(DataSource dataSource, boolean lazyInit) {
setDataSource(dataSource);
setLazyInit(lazyInit);
afterPropertiesSet();
}
- 先说一下如何不用配置使用spring的JdbcTemplate
public class JdbcTemplate{
public static void main(string[] args){
//1.准备数据源:spring内置数据源
DriverManagerDataSource ds=new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/mySpring");
ds.setUsername("root");
ds.setPassword("12345678");
//2.创建JdbcTemplate对象
JdbcTemplate jt=new JdbcTemplate();
jt.setDataSource(ds);//添加数据源
//也可以直接使用有参构造
//JdbcTemplate jt=new JdbcTemplate(ds);
//2.执行操作
jt.execute("insert into account(name,money)values('ccc',500)");
}
}
- 我们已经知道如何使用依赖注入,接下来我们就聊一下如何使用配置加依赖注入的方式来使用JdbcTemplate,在配置之前我们首先要先了解一下数据源
1.3,如何配置数据源
1.3.1,使用spring配置数据源
- 导入两个jar包:
spring-jdbc-5.0.2.RELEASE.jar
和spring-tx-5.0.2.RELEASE.jar
- 配置文件(spring约束)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源-->
<!--可以是c3p0数据源-->
<!--可以是DBCP数据源-->
<!--可以是spring内置数据源-->
</beans>
1.3.2,使用c3p0数据源
- 导入jar包:
c3p0-0.9.2.1.jar
- 配置数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///mySpring"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
1.3.3,使用DBCP数据源
- 导入jar包:
commons-dbcp.jar
和commons-pool.jar
- 配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///mySpring"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
1.3.4,使用spring内置数据源
- 依赖jar包为:
spring-jdbc-5.0.2.REEASE.jar
- 配置数据源
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///mySpring"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
- 配置JdbcTemplate和数据源也已经说完了,接下来就是如何使用JdbcTemplate进行数据的CRUD了
1.4,使用spring的JdbcTemplate进行CRUD
- 1,新建maven工程(spring),导入坐标
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
- 2,用于封装的实体类Account.java
private Integer id;
private String name;
private Float money;
- 3,IAccountDao接口
/**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 查询所有
* */
List<Account> findAll();
/**
* 根据Id查询账户
*/
Account findAccountById(Integer accountId);
/**
* 根据名称查询账户
*/
Account findAccountByName(String accountName);
/**
* 更新账户
*/
void updateAccount(Account account);
}
- 4,AccountDaoImpl.java实现类(重点)
- 注意:
对比下面这两句,发现spring中JdbcTemplate的结果的返回值并不是由BeanPropertyRowMapper
指定,而是由query方法(被多次重载)的返回值决定,这一点和DBUtils中的ResultSetHandler
不一样
DBUtils中的返回结果由ResultSetHandler
决定,而不是query方法的返回值。
List<Account> accounts=jdbcTemplate.query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
Account account = jdbcTemplate.query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements IAccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Account> findAll() {
List<Account> accounts=jdbcTemplate.query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
return accounts;
}
@Override
public Account findAccountById(Integer accountId) {
List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
}
@Override
public Account findAccountByName(String accountName) {
List<Account> accounts = jdbcTemplate.query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
if(accounts.isEmpty()){
return null;
}
if(accounts.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accounts.get(0);
}
@Override
public void updateAccount(Account account) {
jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}
- 5,bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置账户的持久层-->
<bean id="accountDao" class="com.aismall.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mySpring"></property>
<property name="username" value="root"></property>
<property name="password" value="12345678"></property>
</bean>
</beans>
- 测试类JdbcTemplateDemo.java
/**
* JdbcTemplate的最基本用法
*/
public class JdbcTemplateDemo {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
IAccountDao accountDao = ac.getBean("accountDao",IAccountDao.class);
List<Account> accounts=accountDao.findAll();
for (Account account:accounts) {
System.out.println(account);
}
}
}
02,spring中的事务管理(transactionManager)
- 在spring框架学习笔记03中提到了事务控制,那时候是为了展现事务被控制后引起业务层代码过于臃肿,而且业务层和事务控制工具类产生了很多耦合,然后使用动态代理的方式对耦合代码进行抽取,进而引出spring的AOP技术(使用配置的方式实现动态代理,进而实现对重复代码的抽取),但是控制事务使用的却是我们自己编写的工具类,还有就是我们使用的是DBUtils工具类来实现对数据库的交互,而不是使用spring提供的JdbcTemplate
- 在spring框架学习笔记04中提到了如何通过配置的方式实现动态代理(spring的AOP技术)
- 我们在前面提到了如何使用Spring的JdbcTemplate,接下来就是聊一聊如何使用Spring来控制事务,使用Spring的JdbcTemplate与数据库进行交互
- 这样就串起来啦,使用Spring进行事务控制,使用spring的JdbcTemplate与数据库交互,使用Spring的AOP技术实现动态代理(对重复代码进行抽取,然后通过对方法增强的方式将抽取的代码重新织入到抽取的位置)
2.1,Spring 事务控制
第一: JavaEE 体系进行分层开发,事务处理位于业务层, Spring 提供了分层设计业务层的事务处理解决方
案。
第二: spring 框架为我们提供了一组事务控制的接口。具体在后面的第二小节介绍。这组接口是在
spring-tx-5.0.2.RELEASE.jar 中。
第三: spring 的事务控制都是基于 AOP 的,它既可以使用编程的方式实现,也可以使用配置的方式实现。 我
们学习的重点是使用配置的方式实现。
2.2,Spring 中事务控制的 API 介绍
- 先导入jar包:
spring-tx-5.0.2.RELEASE.jar
2.2.1,PlatformTransactionManager接口
PlatformTransactionManager
接口提供了事务操作的方法- 获取事务状态信息的方法:
TransactionStatus getTransaction(TransactionDefinition definition);
- 提交事务的方法:
void commit(TransactionStatus status);
- 回滚事务的方法:
void rollback(TransactionStatus status);
- 注意:
PlatformTransactionManager
只是一个接口,我们要使用此接口的实现类(两个) org.springframework.jdbc.datasource.DataSourceTransactionManager
也就是DataSourceTransactionManager
,使用 Spring的JDBC 或 iBatis 进行持久化数据时使用。(常用)org.springframework.orm.hibernate5.HibernateTransactionManager
也就是HibernateTransactionManager
,使用Hibernate 版本进行持久化数据时使用(不常用)
2.2.2 ,TransactionDefinition
- 在前面的获取事务状态信息的方法中,这个方法的参数是
TransactionDefinition
,
-TransactionDefinition
它是事务的定义信息对象,里面有如下方法: - 获取事务对象名称的方法:
String getName();
- 获取事务隔离级别的方法:
int getIsolationLevel();
- 获取事务传播行为的方法:
int getPropagationBehavior();
- 获取事务超时时间的方法:
int getTimeout();
- 获取事务是否只读的方法:
boolean isReadOnly();
2.2.2.1,事务的隔离级别
事务的四种特性(ACID):
-
1,原子性(atomicity):事务是数据库的逻辑工作单位,而且是必须是原子工作单位,对于其数据修改,要么全部执行,要么全部不执行
-
2,一致性(consistency):事务在完成时,必须是所有的数据都保持一致状态。在相关数据库中,所有规则都必须应用于事务的修改,以保持所有数据的完整性。(实例:转账,两个账户余额相加,值不变)
-
3, 隔离性(isolation):一个事务的执行不能被其他事务所影响。
-
4,持久性(durability):一个事务一旦提交,事物的操作便永久性的保存在DB中。即便是在数据库系统遇到故障的情况下也不会丢失提交事务的操作
事务的隔离级别分五种 -
1,ISOLATION_DEFAULT:这是一个,默认的隔离级别,使用数据库默认的事务隔离级别.
-
另外四个与JDBC的隔离级别相对应;
-
2,ISOLATION_READ_UNCOMMITTED:这是事务最低的隔离级别,它允许别外一个事务可以看到这个事务未提交的数据。
这种隔离级别会产生脏读,不可重复读和幻像读。 -
3,ISOLATION_READ_COMMITTED:保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。
这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。 -
4,ISOLATION_REPEATABLE_READ:这种事务隔离级别可以防止脏读,不可重复读。
但是可能出现幻像读。它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。 -
5,ISOLATION_SERIALIZABLE:这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。
除了防止脏读,不可重复读外,还避免了幻像读。
2.2.2.2,事务的传播行为
- REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)
- SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
- MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常
- REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。
- NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
- NEVER:以非事务方式运行,如果当前存在事务,抛出异常
- NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作。
2.2.2.3,超时时间
- 默认值是-1,没有超时限制。如果有,以秒为单位进行设置。
2.2.2.4,是否为只读事务
- 一般查询时事务才设置为只读
2.2.3,TransactionStatus接口
- 此接口提供的是事务具体的运行状态,接口中包含6个方法
- 刷新事务
void flush();
- 获取是否存在存储点
boolean hasSavepoint();
- 获取是否是否完成
boolean isCompleted();
- 获取事务是否为新事务
boolean isNewTransaction();
- 获取事务是否回滚
boolean isRollbackOnly();
- 设置事务回滚
void setRollbackOnly();
2.3,spring的事务控制配置(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: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>
配置步骤
- 1、配置事务管理器
- 2、配置事务的通知
此时我们需要导入事务的约束 tx名称空间和约束,同时也需要aop的
使用tx:advice
标签配置事务通知
属性:
id:给事务通知起一个唯一标识
transaction-manager:给事务通知提供一个事务管理器引用 - 3、配置AOP中的通用切入点表达式
- 4、建立事务通知和切入点表达式的对应关系
- 5、配置事务的属性
是在事务的通知tx:advice标签的内部
配置事务的属性
- isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
- propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
- read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
- timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
- rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
- no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。
03,案例演示
- 前面说了这么多,最后用一个案例来演示一下吧
- 案例中:使用Spring的JdbcTemplate访问数据库,使用Spring进行事务控制(基于XML配置方式),使用Spring的AOP实现动态代理
3.1,新建一个maven工程(spring)
- 导入坐标:
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
3.2,IAccountDao接口和AccountDaoImpl实现类
- IAccountDao
/**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 根据Id查询账户
*/
Account findAccountById(Integer accountId);
/**
* 根据名称查询账户
*/
Account findAccountByName(String accountName);
/**
* 更新账户
*/
void updateAccount(Account account);
}
- AccountDaoImpl(此处使用的Spring的JdbcTemplate)
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
@Override
public Account findAccountById(Integer accountId) {
List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
}
@Override
public Account findAccountByName(String accountName) {
List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
if(accounts.isEmpty()){
return null;
}
if(accounts.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accounts.get(0);
}
@Override
public void updateAccount(Account account) {
super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}
3.3,实体类Account
private Integer id;
private String name;
private Float money;
3.4,IAccountService接口和AccountServiceImpl实现类
- AccountService
public interface IAccountService {
/**
* 根据id查询账户信息
*/
Account findAccountById(Integer accountId);
/**
* 转账
* @param sourceName 转成账户名称
* @param targetName 转入账户名称
* @param money 转账金额
*/
void transfer(String sourceName, String targetName, Float money);
}
- AccountServiceImpl(此处以前使用动态代理,现在使用AOP)
/**
* 账户的业务层实现类
*
* 事务控制应该都是在业务层
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Override
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer....");
//2.1根据名称查询转出账户
Account source = accountDao.findAccountByName(sourceName);
//2.2根据名称查询转入账户
Account target = accountDao.findAccountByName(targetName);
//2.3转出账户减钱
source.setMoney(source.getMoney()-money);
//2.4转入账户加钱
target.setMoney(target.getMoney()+money);
//2.5更新转出账户
accountDao.updateAccount(source);
//int i=1/0;
//2.6更新转入账户
accountDao.updateAccount(target);
}
}
3.5,bean.xm(重点在这里)
<?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/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="accountService" class="com.aismall.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置账户的持久层-->
<bean id="accountDao" class="com.aismall.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mySpring"></property>
<property name="username" value="root"></property>
<property name="password" value="12345678"></property>
</bean>
<!-- spring中基于XML的声明式事务控制配置步骤:-->
<!-- 第一步:配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 第二步:配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 第五步:配置事务的属性-->
<!--配置处理不同事务所用的方法-->
<!--全统配符优先级小于部分通配符的优先级-->
<tx:attributes>
<!--处理增删改的方法:一定会发生事务,读写-->
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<!--处理查询的方法:不一定发生事务,只读-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置aop-->
<aop:config>
<!-- 第三步:配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution(* com.aismall.service.impl.*.*(..))"></aop:pointcut>
<!--第四步:建立切入点表达式和事务通知的对应关系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
3.6,测试类
/**
* 使用Junit单元测试:测试我们的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testTransfer(){
as.transfer("aaa","bbb",100f);
}
}