Spring_day3
AOP 概述
- 简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有方法进行增强。
- AOP 的作用及优势
- 作用:
在程序运行期间,不修改源码对已有方法进行增强。 - 优势:
减少重复代码
提高开发效率
维护方便
- 作用:
动态代理常用的有两种方式
- 基于接口的动态代理
提供者:JDK 官方的 Proxy 类。
要求:被代理类最少实现一个接口。 - 基于子类的动态代理
提供者:第三方的 CGLib,如果报 asmxxxx 异常,需要导入 asm.jar。
要求:被代理类不能用 final 修饰的类(最终类)。
使用 CGLib 的 Enhancer 类创建代理对象
/**
* 一个演员
*/
public class Actor{//没有实现任何接口public void basicAct(float money){System.out.println("拿到钱,开始基本的表演:"+money);
}public void dangerAct(float money){System.out.println("拿到钱,开始危险的表演:"+money);}
}
public class Client {/*** 基于子类的动态代理* 要求:* 被代理对象不能是最终类* 用到的类:* Enhancer* 用到的方法:* create(Class, Callback)* 方法的参数:* Class:被代理对象的字节码* Callback:如何代理* @param args*/public static void main(String[] args) {final Actor actor = new Actor();Actor cglibActor = (Actor) Enhancer.create(actor.getClass(),
new MethodInterceptor() {/*** 执行被代理对象的任何方法,都会经过该方法。在此方法内部就可以对被代理对象的任何
方法进行增强。* * 参数:* 前三个和基于接口的动态代理是一样的。* MethodProxy:当前执行方法的代理对象。* 返回值:* 当前执行方法的返回值*/@Overridepublic Object intercept(Object proxy, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {String name = method.getName();Float money = (Float) args[0];Object rtValue = null;if("basicAct".equals(name)){//基本演出if(money > 2000){rtValue = method.invoke(actor, money/2);}}if("dangerAct".equals(name)){//危险演出if(money > 5000){rtValue = method.invoke(actor, money/2);}}return rtValue;}});cglibActor.basicAct(10000);cglibActor.dangerAct(100000);}
}
Spring 中的 AOP
AOP 相关术语
- Joinpoint(连接点):
所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring 只支持方法类型的连接点。
- Pointcut(切入点):
所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。
- Advice(通知/增强):
所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知。
通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。
- Introduction(引介):
引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
- Target(目标对象):
代理的目标对象。
- Weaving(织入):
是指把增强应用到目标对象来创建新的代理对象的过程。
spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。
- Proxy(代理):
一个类被 AOP 织入增强后,就产生一个结果代理类。
- Aspect(切面):
是切入点和通知(引介)的结合。
基于 XML 的 AOP 配置
- 第一步:导入spring坐标
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.4.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency></dependencies>
- 不要忘了导入 aop 的依赖 aspectJ 的依赖包
- 第二步:创建 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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
- 第三步:配置 spring 的 ioc
<bean id="UserService" class="cn.myp666.service.impl.UserServiceImpl"></bean><bean id="UserLog" class="cn.myp666.service.UserLog"></bean>
<!--
<aop:config><aop:pointcut id="lg1" expression="execution( public void cn.myp666.service.impl.UserServiceImpl.saveUser())"/><aop:aspect id="logAdvice" ref="UserLog"><aop:before method="printLog" pointcut-ref="lg1"></aop:before></aop:aspect></aop:config>--><aop:config><aop:aspect id="logAdvice" ref="UserLog"><aop:before method="printLog" pointcut="execution( public void cn.myp666.service.impl.UserServiceImpl.saveUser())"></aop:before></aop:aspect></aop:config>
- aop:config:
作用:用于声明开始 aop 的配置
< aop:config >
< !-- 配置的代码都写在此处 – >
</aop:config>
- aop:aspect:
作用:
用于配置切面。
属性:
id:给切面提供一个唯一标识。
ref:引用配置好的通知类 bean 的 id。
- aop:pointcut:
作用:
用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
属性:
expression:用于定义切入点表达式。
id:用于给切入点表达式提供一个唯一标识
<aop:pointcut id="lg1" expression="execution( public void cn.myp666.service.impl.UserServiceImpl.saveUser())"/><aop:aspect id="logAdvice" ref="UserLog"><aop:before method="printLog" pointcut-ref="lg1"></aop:before></aop:aspect>
- aop:before
作用:
用于配置前置通知。指定增强的方法在切入点方法之前执行
属性:
method:用于指定通知类中的增强方法名称
ponitcut-ref:用于指定切入点的表达式的引用
poinitcut:用于指定切入点表达式
- aop:after-returning
作用:
用于配置后置通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
- aop:after-throwing
作用:
用于配置异常通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
- aop:after
作用:
用于配置最终通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
-
execution: 匹配方法的执行(常用)
- execution(表达式)
-
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
-
写法说明:
- public void cn.myp666.service.impl.UserServiceImpl.saveUser()
- 全通配方式:
* *..*.*(..)
-
通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。
execution(* cn.myp666.service.impl.*.*(..))