Spring AOP 的使用详解
1、AOP简介
AOP (Aspect Orient Programming),直译过来就是 面向切面编程。通俗地说,aop就是在不影响业务代码的情况下,实现对现有业务的一些改造,或者在现有功能的基础上新增一些附加的功能,例如:日志打印、异常处理等。
2、有关名词释义
- 通知(Advice): AOP 框架中的增强处理。通知描述了切面何时执行以及如何执行增强处理。
- 连接点(join point): 连接点表示应用执行过程中能够插入切面的一个点,这个点可以是方法的调用、异常的抛出。在 Spring AOP 中,连接点总是方法的调用。
- 切点(PointCut): 可以插入增强处理的连接点。
- 切面(Aspect): 切面是通知和切点的结合。
- 引入(Introduction):引入允许我们向现有的类添加新的方法或者属性。
- 织入(Weaving): 将增强处理添加到目标对象中,并创建一个被增强的对象,这个过程就是织入。
3、通知类型
通知类型 | 接口 | 描述 |
---|---|---|
前置通知 | org.springframework.aop.MethodBeforeAdvice | 在目标方法执行前实施增强。 |
后置通知 | org.springframework.aop.AfterReturningAdvice | 在目标方法执行后实施增强。 |
后置返回通知 | org.springframework.aop.AfterReturningAdvice | 在目标方法执行完成,并返回一个返回值后实施增强。 |
环绕通知 | org.aopalliance.intercept.MethodInterceptor | 在目标方法执行前后实施增强。 |
异常通知 | org.springframework.aop.ThrowsAdvice | 在方法抛出异常后实施增强。 |
引入通知 | org.springframework.aop.IntroductionInterceptor | 在目标类中添加一些新的方法和属性。 |
4、Demo 编写
package com.example.aopdemo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.lang.reflect.Method;
@Slf4j
@Aspect
@Component
public class LogAspect {
@Pointcut("execution (* com.example.aopdemo.TestController.*(..))")
public void printLog(){
System.out.println("我是一个切点");
}
/*使用aop 进行请求拦截,获取请求头和请求参数并打印*/
@Before("printLog()")
public void before(JoinPoint joinPoint){
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
log.info("URL参数={}",requestAttributes.getRequest().getQueryString());
String header = requestAttributes.getRequest().getHeader("userId");
System.out.println("header is : "+header);
}
@After("printLog()")
public void after(){
System.out.println("我是一个after");
}
/*使用aop 进行入参的拦截,修改入参,修改返回值*/
@Around("printLog()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("--------Around方法开始执行");
Gson gson = new Gson();
Object[] args = proceedingJoinPoint.getArgs();
System.out.println("参数:"+args[0]);
// 将获取的值进行修改
args[0] = "我是你爸爸"; //修改参数值
// 然后继续执行该接口(这里修改参数)
Object ret = proceedingJoinPoint.proceed(args); //将新的参数传入
return args[0]; //返回新的返回值,类型与方法原来的返回值相同
}
}
注意:ProceedingJoinPoint 只能在Around 类的通知类型中使用。
5、切点表达式
- 在Spring AOP中,连接点始终代表方法的执行。切入点是与连接点匹配的,切入点表达语言是以编程方式描述切入点的方式。
- 切入点(Poincut)是定义了在“什么地方”进行切入,哪些连接点会得到通知。显然,切点一定是连接点
- 切点是通过
@Pointcut
注解和切点表达式
定义的。@Pointcut注解可以在一个切面内定义可重用
的切点。
execute表达式
*
代表匹配任意修饰符及任意返回值,参数列表中..
匹配任意数量的参数
可以使用&&、||、!、三种运算符来组合切点表达式,表示与或非的关系
- 拦截任意公共方法
execution(public * *(..))
- 拦截以set开头的任意方法
execution(* set*(..))
- 拦截类或者接口中的方法
//拦截AccountService(类、接口)中定义的所有方法
execution(* com.xyz.service.AccountService.*(..))
- 拦截包中定义的方法,不包含子包中的方法
//拦截com.xyz.service包中所有类中任意方法,不包含子包中的类
execution(* com.xyz.service.*.*(..))
- 拦截包或者子包中定义的方法
//拦截com.xyz.service包或者子包中定义的所有方法
execution(* com.xyz.service..*.*(..))
6、JoinPoint和ProceedingJoinPoint对象
JoinPoint
对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.ProceedingJoinPoint
对象是JoinPoint的子接口,该对象只用在@Around的切面方法中
方法名 | 功能 |
---|---|
Signature getSignature(); | 获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息 |
Object[] getArgs(); | 获取传入目标方法的参数对象 |
Object getTarget(); | 获取被代理的对象 |
Object getThis(); | 获取代理对象 |
@Aspect
@Component
public class aopAspect {
/**
* 定义一个切入点表达式,用来确定哪些类需要代理
* execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理
*/
@Pointcut("execution(* aopdemo.*.*(..))")
public void declareJoinPointerExpression() {}
/**
* 前置方法,在目标方法执行前执行
* @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
*/
@Before("declareJoinPointerExpression()")
public void beforeMethod(JoinPoint joinPoint){
System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());
System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
//获取传入目标方法的参数
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
System.out.println("第" + (i+1) + "个参数为:" + args[i]);
}
System.out.println("被代理的对象:" + joinPoint.getTarget());
System.out.println("代理对象自己:" + joinPoint.getThis());
}
/**
* 环绕方法,可自定义目标方法执行的时机
* @param pjd JoinPoint的子接口,添加了
* Object proceed() throws Throwable 执行目标方法
* Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法
* 两个方法
* @return 此方法需要返回值,返回值视为目标方法的返回值
*/
@Around("declareJoinPointerExpression()")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result = null;
try {
//前置通知
System.out.println("目标方法执行前...");
//执行目标方法
//result = pjd.proeed();
//用新的参数值执行目标方法
result = pjd.proceed(new Object[]{"newSpring","newAop"});
//返回通知
System.out.println("目标方法返回结果后...");
} catch (Throwable e) {
//异常通知
System.out.println("执行目标方法异常后...");
throw new RuntimeException(e);
}
//后置通知
System.out.println("目标方法执行后...");
return result;
}
}