1.概述
- 1)在具体的连接点上要执行的操作。
- 2)一个切面可以包括一个或者多个通知。
- 3)通知所使用的注解的值往往是切入点表达式。
2.前置通知
- 1)前置通知:在方法执行之前执行的通知
- 2)使用@Before注解
/**
* @Before 将方法指定为前置通知
* 必须设置value,其值为切入点表达式
* @Before(value = "execution(* com.atguigu.spring.day03_Spring.aop.*.*(..))")
*第一个*代表任意修饰符和返回值类型
*第二个*代表任意类中
*第三个*代表类中任意方法
* ..代表任意参数返回值列表
*/
//@Before(value = "execution(public int com.atguigu.spring.day03_Spring.aop.MathImpl.add(int,int))")
@Before(value = "execution(* com.atguigu.spring.day03_Spring.aop.*.*(..))")
public void beforeMethod(JoinPoint joinPoint){
//获取方法的参数
Object[] args = joinPoint.getArgs();
//获取方法名
String name = joinPoint.getSignature().getName();
System.out.println("method: "+ name + ",arguments: "+ Arrays.toString(args));
}
3.后置通知
- 1)后置通知:后置通知是在连接点完成之后执行的,即连接点返回结果或者抛出异常的时候
- 2)使用@After注解
/**
* @After 将方法指定为后置通知
* 作用于方法的finally语句块,即不管有没有异常都会执行
*/
@After(value = "execution(* com.atguigu.spring.day03_Spring.aop.*.*(..))")
public void afterMethd(JoinPoint joinPoint){
System.out.println("后置通知!");
}
4.返回通知
- 1)返回通知:无论连接点是正常返回还是抛出异常,后置通知都会执行。如果只想在连接点返回的时候记录日志,应使用返回通知代替后置通知。
- 2)使用@AfterReturning注解,在返回通知中访问连接点的返回值
①在返回通知中,只要将returning属性添加到@AfterReturning注解中,就可以访问连接点的返回值。该属性的值即为用来传入返回值的参数名称
②必须在通知方法的签名中添加一个同名参数。在运行时Spring AOP会通过这个参数传递返回值
③原始的切点表达式需要出现在pointcut属性中
/**
* @AfterReturning 将方法指定为返回通知
*作用于方法执行之后
* 可通过returning设置接收方法返回值的变量名
* 要想在方法中使用,必须在方法的形参中设置和变量名相同的参数名的参数
*/
@AfterReturning(value = "execution(* com.atguigu.spring.day03_Spring.aop.*.*(..))",returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result){
String name = joinPoint.getSignature().getName();
System.out.println("method: "+name +", result: "+ result);
}
5.异常通知
- 1)异常通知:只在连接点抛出异常时才执行异常通知
- 2)将throwing属性添加到@AfterThrowing注解中,也可以访问连接点抛出的异常。Throwable是所有错误和异常类的顶级父类,所以在异常通知方法可以捕获到任何错误和异常。
- 3)如果只对某种特殊的异常类型感兴趣,可以将参数声明为其他异常的参数类型。然后通知就只在抛出这个类型及其子类的异常时才被执行
/**
* @AfterThrowing 将方法标注为异常通知
* 当方法抛出异常时作用
* 可通过throwing设置接收方法返回的异常通知
* 在参数列表中可通过具体的异常类型,来对指定的异常信息进行操作
*/
@AfterThrowing(value = "execution(* com.atguigu.spring.day03_Spring.aop.*.*(..))",throwing = "ex")
public void afterThrowing(Exception ex){
System.out.println("有异常了! messages: " + ex);
}
6.环绕通知
- 1)环绕通知是所有通知类型中功能最为强大的,能够全面地控制连接点,甚至可以控制是否执行连接点。
- 2)对于环绕通知来说,连接点的参数类型必须是ProceedingJoinPoint。它是 JoinPoint的子接口,允许控制何时执行,是否执行连接点。
- 3)在环绕通知中需要明确调用ProceedingJoinPoint的proceed()方法来执行被代理的方法。如果忘记这样做就会导致通知被执行了,但目标方法没有被执行。
- 4)注意:环绕通知的方法需要返回目标方法执行之后的结果,即调用 joinPoint.proceed();的返回值,否则会出现空指针异常。
@Around(value = "execution(* com.atguigu.spring.day03_Spring.aop.*.*(..))")
public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint){
Object result = null;
try {
//前置通知
System.out.println("前置通知");
result = proceedingJoinPoint.proceed();//执行方法
//返回通知
System.out.println("返回通知");
return result;
} catch (Throwable throwable) {
throwable.printStackTrace();
//异常通知
System.out.println("异常通知");
}finally {
//后置通知
System.out.println("后置通知");
}
return -1;
}