快速学习-Spring 中的 AOP
- 2020 年 4 月 2 日
- 筆記
第2章 Spring 中的 AOP[掌握]
2.1 Spring 中 AOP 的细节
2.1.1 说明
我们学习 spring 的 aop,就是通过配置的方式,实现上一章节的功能。
2.1.2 AOP 相关术语
Joinpoint(连接点): 所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring 只支持方法类型的连接点。
Pointcut(切入点): 所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。
Advice(通知/增强): 所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知。 通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。
Introduction(引介): 引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
Target(目标对象): 代理的目标对象。
Weaving(织入): 是指把增强应用到目标对象来创建新的代理对象的过程。spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。
Proxy(代理): 一个类被 AOP 织入增强后,就产生一个结果代理类。
Aspect(切面): 是切入点和通知(引介)的结合。
2.1.3 学习 spring 中的 AOP 要明确的事
a、开发阶段(我们做的) 编写核心业务代码(开发主线):大部分程序员来做,要求熟悉业务需求。把公用代码抽取出来,制作成通知。(开发阶段最后再做):AOP 编程人员来做。 在配置文件中,声明切入点与通知间的关系,即切面。:AOP 编程人员来做。 b、运行阶段(Spring 框架完成的) Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。
2.1.4 关于代理的选择
在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
2.2 基于 XML 的 AOP 配置
示例: 我们在学习 spring 的 aop 时,采用账户转账作为示例。并且把 spring 的 ioc 也一起应用进来。
2.2.1 环境搭建
2.2.1.1 第一步:准备必要的代码
此处包含了实体类,业务层和持久层代码。我们沿用上一章节中的代码即可。
2.2.1.2 第二步:拷贝必备的 jar 包到工程的 lib 目录
此处要拷贝 spring 的 ioc 和 aop 两组 jar 包

2.2.1.3 第三步:创建 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" xmlns:aop="http://www.springframework.org/schema/aop" 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"> </beans>
2.2.1.4 第四步:配置 spring 的 ioc
<!-- 配置 service --> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置 dao --> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="dbAssit" ref="dbAssit"></property> </bean> <!-- 配置数据库操作对象 --> <bean id="dbAssit" class="com.itheima.dbassit.DBAssit"> <property name="dataSource" ref="dataSource"></property> <!-- 指定 connection 和线程绑定 --> <property name="useCurrentConnection" value="true"></property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean>
2.2.1.5 第五步:抽取公共代码制作成通知
public class TransactionManager { //定义一个 DBAssit private DBAssit dbAssit; public void setDbAssit(DBAssit dbAssit) { this.dbAssit = dbAssit; } //开启事务 public void beginTransaction() { try { dbAssit.getCurrentConnection().setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } } //提交事务 public void commit() { try { dbAssit.getCurrentConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } } //回滚事务 public void rollback() { try { dbAssit.getCurrentConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } } //释放资源 public void release() { try { dbAssit.releaseConnection(); } catch (Exception e) { e.printStackTrace(); } } }
2.2.2 配置步骤
2.2.2.1 第一步:把通知类用 bean 标签配置起来
<!-- 配置通知 --> <bean id="txManager" class="com.itheima.utils.TransactionManager"> <property name="dbAssit" ref="dbAssit"></property> </bean>
2.2.2.2 第二步:使用 aop:config 声明 aop 配置
aop:config: 作用:用于声明开始 aop 的配置
<aop:config> <!-- 配置的代码都写在此处 --> </aop:config>
2.2.2.3 第三步:使用 aop:aspect 配置切面
aop:aspect: 作用: 用于配置切面。 属性: id:给切面提供一个唯一标识。 ref:引用配置好的通知类 bean 的 id。
<aop:aspect id="txAdvice" ref="txManager"> <!--配置通知的类型要写在此处--> </aop:aspect>
2.2.2.4 第四步:使用 aop:pointcut 配置切入点表达式
aop:pointcut: 作用: 用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。 属性: expression:用于定义切入点表达式。 id:用于给切入点表达式提供一个唯一标识
<aop:pointcut expression="execution( public void com.itheima.service.impl.AccountServiceImpl.transfer( java.lang.String, java.lang.String, java.lang.Float) )" id="pt1"/>
2.2.2.5 第五步:使用 aop:xxx 配置对应的通知类型
aop:before 作用: 用于配置前置通知。指定增强的方法在切入点方法之前执行 属性:
method:用于指定通知类中的增强方法名称 ponitcut-ref:用于指定切入点的表达式的引用 poinitcut:用于指定切入点表达式 执行时间点: 切入点方法执行之前执行 <aop:before method="beginTransaction" pointcut-ref="pt1"/> aop:after-returning 作用: 用于配置后置通知 属性: method:指定通知中方法的名称。 pointct:定义切入点表达式 pointcut-ref:指定切入点表达式的引用 执行时间点: 切入点方法正常执行之后。它和异常通知只能有一个执行 <aop:after-returning method="commit" pointcut-ref="pt1"/> aop:after-throwing 作用: 用于配置异常通知 属性: method:指定通知中方法的名称。 pointct:定义切入点表达式 pointcut-ref:指定切入点表达式的引用 执行时间点: 切入点方法执行产生异常后执行。它和后置通知只能执行一个 <aop:after-throwing method="rollback" pointcut-ref="pt1"/> aop:after 作用: 用于配置最终通知 属性: method:指定通知中方法的名称。 pointct:定义切入点表达式 pointcut-ref:指定切入点表达式的引用 执行时间点: 无论切入点方法执行时是否有异常,它都会在其后面执行。 <aop:after method="release" pointcut-ref="pt1"/>
2.2.3 切入点表达式说明
execution:匹配方法的执行(常用) execution(表达式) 表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数)) 写法说明: 全匹配方式: public void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account) 访问修饰符可以省略 void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account) 返回值可以使用*号,表示任意返回值 * com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account) 包名可以使用*号,表示任意包,但是有几级包,需要写几个* * *.*.*.*.AccountServiceImpl.saveAccount(com.itheima.domain.Account) 使用..来表示当前包,及其子包 * com..AccountServiceImpl.saveAccount(com.itheima.domain.Account) 类名可以使用*号,表示任意类 * com..*.saveAccount(com.itheima.domain.Account) 方法名可以使用*号,表示任意方法 * com..*.*( com.itheima.domain.Account) 参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数 * com..*.*(*) 参数列表可以使用..表示有无参数均可,有参数可以是任意类型 * com..*.*(..) 全通配方式: * *..*.*(..) 注: 通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。 execution(* com.itheima.service.impl.*.*(..))
2.2.4 环绕通知
配置方式: <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/> <aop:aspect id="txAdvice" ref="txManager"> <!-- 配置环绕通知 --> <aop:around method="transactionAround" pointcut-ref="pt1"/> </aop:aspect> </aop:config> aop:around: 作用: 用于配置环绕通知 属性: method:指定通知中方法的名称。 pointct:定义切入点表达式 pointcut-ref:指定切入点表达式的引用 说明: 它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。 注意: 通常情况下,环绕通知都是独立使用的
/** * 环绕通知 * @param pjp * spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。 * 在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。 * @return */ public Object transactionAround(ProceedingJoinPoint pjp) { //定义返回值 Object rtValue = null; try { //获取方法执行所需的参数 Object[] args = pjp.getArgs(); //前置通知:开启事务 beginTransaction(); //执行方法 rtValue = pjp.proceed(args); //后置通知:提交事务 commit(); } catch (Throwable e) { //异常通知:回滚事务 rollback(); e.printStackTrace(); } finally { //最终通知:释放资源 release(); } return rtValue; }
2.3 基于注解的 AOP 配置
2.3.1 环境搭建
2.3.1.1 第一步:准备必要的代码和 jar 包
拷贝上一小节的工程即可。
2.3.1.2 第二步:在配置文件中导入 context 的名称空间
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置数据库操作对象 --> <bean id="dbAssit" class="com.itheima.dbassit.DBAssit"> <property name="dataSource" ref="dataSource"></property> <!-- 指定 connection 和线程绑定 --> <property name="useCurrentConnection" value="true"></property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean> </beans>
2.3.1.3 第三步:把资源使用注解配置
@Service("accountService") public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; } @Repository("accountDao") public class AccountDaoImpl implements IAccountDao { @Autowired private DBAssit dbAssit; }
2.3.1.4 第四步:在配置文件中指定 spring 要扫描的包
<!-- 告知 spring,在创建容器时要扫描的包 --> <context:component-scan base-package="com.itheima"></context:component-scan>
2.3.2 配置步骤
2.3.2.1 第一步:把通知类也使用注解配置
@Component("txManager") public class TransactionManager { //定义一个 DBAssit @Autowired private DBAssit dbAssit; }
2.3.2.2 第二步:在通知类上使用@Aspect 注解声明为切面
作用: 把当前类声明为切面类。
@Component("txManager") @Aspect //表明当前类是一个切面类 public class TransactionManager { //定义一个 DBAssit @Autowired private DBAssit dbAssit; }
2.3.2.3 第三步:在增强的方法上使用注解配置通知
@Before 作用: 把当前方法看成是前置通知。 属性: value: 用于指定切入点表达式, 还可以指定切入点表达式的引用。 //开启事务 @Before("execution(* com.itheima.service.impl.*.*(..))") public void beginTransaction() { try { dbAssit.getCurrentConnection().setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } } @AfterReturning 作用: 把当前方法看成是后置通知。 属性: value: 用于指定切入点表达式, 还可以指定切入点表达式的引用 //提交事务 @AfterReturning("execution(* com.itheima.service.impl.*.*(..))") public void commit() { try { dbAssit.getCurrentConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } } @AfterThrowing 作用: 把当前方法看成是异常通知。 属性: value: 用于指定切入点表达式, 还可以指定切入点表达式的引用 //回滚事务 @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))") public void rollback() { try { dbAssit.getCurrentConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } } @After 作用: 把当前方法看成是最终通知。 属性: value: 用于指定切入点表达式, 还可以指定切入点表达式的引用 //释放资源 @After("execution(* com.itheima.service.impl.*.*(..))") public void release() { try { dbAssit.releaseConnection(); } catch (Exception e) { e.printStackTrace(); } }
2.3.2.4 第四步:在 spring 配置文件中开启 spring 对注解 AOP 的支持
<!-- 开启 spring 对注解 AOP 的支持 --> <aop:aspectj-autoproxy/>
2.3.3 环绕通知注解配置
@Around 作用: 把当前方法看成是环绕通知。 属性: value: 用于指定切入点表达式, 还可以指定切入点表达式的引用。 /** * 环绕通知 * @param pjp * @return */ @Around("execution(* com.itheima.service.impl.*.*(..))") public Object transactionAround(ProceedingJoinPoint pjp) { //定义返回值 Object rtValue = null; try { //获取方法执行所需的参数 Object[] args = pjp.getArgs(); //前置通知:开启事务 beginTransaction(); //执行方法 rtValue = pjp.proceed(args); //后置通知:提交事务 commit(); } catch (Throwable e) { //异常通知:回滚事务 rollback(); e.printStackTrace(); } finally { //最终通知:释放资源 release(); } return rtValue; }
2.3.4 切入点表达式注解
@Pointcut 作用: 指定切入点表达式 属性: value: 指定表达式的内容 @Pointcut("execution(* com.itheima.service.impl.*.*(..))") private void pt1() {} 引用方式: /** * 环绕通知 * @param pjp * @return */ @Around("pt1()") //注意:千万别忘了写括号 public Object transactionAround(ProceedingJoinPoint pjp) { //定义返回值 Object rtValue = null; try { //获取方法执行所需的参数 Object[] args = pjp.getArgs(); //前置通知:开启事务 beginTransaction(); //执行方法 rtValue = pjp.proceed(args); //后置通知:提交事务 commit(); } catch (Throwable e) { //异常通知:回滚事务 rollback(); e.printStackTrace(); } finally { //最终通知:释放资源 release(); } return rtValue; }
2.3.5 不使用 XML 的配置方式
@Configuration @ComponentScan(basePackages="com.itheima") @EnableAspectJAutoProxy public class SpringConfiguration { }