spring aop概述
spring aop
作用:将与具体业务模块无关、但多个模块中都会使用的操作抽取出来,集中处理
实现原理:spring aop使用动态代理实现aop功能,支持jdk动态代理、cglib动态代理
jdk动态代理:目标对象需要实现接口,利用反射获得接口,生成实现接口的代理类,构造代理对象,调用代理对象方法
cglib动态代理:不需要实现接口,利用asm开源包,修改被代理对象类的字节码创建代理类
springboot aop代理方式控制
# springboot代理方式可通过spring.aop.proxy-target-class控制
false:存在接口时,使用jdk动态代理;不存在接口时,使用cglib动态代理
true:不论是否存在接口,都强制使用cglib代理
# springboot默认行为
springboot 1.x:proxy-target-class默认为false
springboot 2.x:proxy-target-class默认为true
# additional-spring-configuration-metadata.json
{
"properties": [
{
"name": "spring.aop.proxy-target-class",
"type": "java.lang.Boolean",
"description": "Whether subclass-based (CGLIB) proxies are to be created (true), as opposed to standard Java interface-based proxies (false).",
"defaultValue": true
},
]
}
基本概念
切面:通知+切点,在何时、何处做什么
通知:在何时做什么,共有五种类型:before、after、after-returning、after-throwing、around
切点:定义了在何处应用通知
连接点:可以应用通知的地方,spring aop中的连接点为任意方法
引入:为现有的类添加方法
织入:将切面应用到目标对象,并创建代理对象的过程
切面织入时间
编译期:切面在目标类编译时织入,AspectJ的织入编译器使用这种方式织入
类加载期:切面在类加载期应用织入,AspectJ5使用这种方式织入
运行期:切面在应用运行期织入,spring aop使用这种方式织入
切点表达式
表达式运算符
&&:同时满足多个条件
||:满足当中一个条件即可
! :否定操作
@Pointcut("within(com.example.demo.service..*) && bean(helloServiceImpl)")
* 当前调用对象的目标对象是包com.example.demo.service或者子包下面的类,并且实例名称是helloServiceImpl时,切点匹配
@Pointcut("within(com.example.demo.service..*) || bean(helloServiceImpl)")
* 当前调用对象的目标对象是包com.example.demo.service或者子包下面的类,或者实例名称是helloServiceImpl时,切点匹配
@Pointcut("!bean(helloServiceImpl)")
* 当前调用对象的实例名称不是helloServiceImpl时,切点匹配
execution:调用的方法匹配切点表达式时,切点匹配
格式:execution(修饰符(可无) 返回类型 包名+类名+方法名(参数体) 异常体(可无))
*:通配符匹配任意字符
(..):匹配任意方法体
@Pointcut("execution(* *.hello(..))")
* 连接点匹配的方法:返回值为任意类型,方法名为hello,参数体为任意参数
this:当前调用对象是指定的类或者其子类时,切点匹配
格式:this(包名.类名)
@Pointcut("this(com.example.demo.service.impl.HelloServiceImpl)")
* 当调用的对象是HelloServiceImpl或者其子类时,切点匹配
target、@target
target:当前调用对象(代理对象)的目标对象(被代理对象)是指定的类或者其子类时,切点匹配
@target:当前调用对象的目标对象被指定注解修饰时,切点匹配
@Pointcut("target(com.example.demo.service.HelloService+)")
* 当前对象的目标对象是HelloService类或者其子类时,切点匹配
* HelloService+:表示HelloService类及其子类
@Pointcut("target(com.example.demo.service.impl.HelloServiceImpl)")
* 当前对象的目标对象是HelloServiceImpl类或者其子类时,切点匹配
@Pointcut("within(com.example.demo.service..*)" +
"&& @target(com.example.demo.annotation.CustomAnnotation4)")
* 当前调用对象的目标对象在com.example.demo.service包下,
* 并且被CustomAnnotation注解修饰时,切点匹配
说明:@target不能单独使用,会报错
within、@within
within:当前调用对象的目标对象(被代理对象)为指定的类(同target)、或者包下所有的类(target无此用法,会报错)
@within:当前调用对象的目标对象标注了指定注解
@Pointcut("within(com.example.demo.service.HelloService)")
* 当前调用对象的目标对象是HelloService或者其子类时,切点匹配
@Pointcut("within(com.example.demo.service..*)")
* 当前调用对象的目标对象是包com.example.demo.service及子包下的类时,切点匹配
@Pointcut("@within(com.example.demo.annotation.CustomAnnotation)")
* 当前调用对象的目标对象上标注有CustomAnnotation注解
args、@args
args:方法的参数体为指定类型时,切点匹配;同时可以使用args向通知传递参数
@args:方法的参数对应的类上被指定的注解标注时,切点匹配
注意:args、@args不能单独使用,需与其他切点表达式共同使用
@Pointcut("within(com.example.demo.service..*) && args(java.lang.String)")
* 包com.example.demo.service及子包下所有的类,
* 且方法只有一个参数,参数类型为java.lang.String时,切点匹配
@Pointcut("within(com.example.demo.service..*) "+
"&& @args(com.example.demo.annotation.CustomAnnotation)")
* 包com.example.demo.service及子包下所有的类,
* 且方法只有一个参数,参数对应的类标注注解CustomAnnotation时,切点匹配
@annotation:调用的方法被指定的注解标注时,切点匹配
@Pointcut("@annotation(com.example.demo.annotation.CustomAnnotation)")
* 调用的方法标注有注解CustomAnnotation时,切点匹配
bean:调用对象的实例名称是指定名称时,切点匹配
格式:bean(beanName)
@Pointcut("bean(helloServiceImpl)")
* 当前调用对象的名称是helloServiceImpl时,切点匹配