SpringAOP02_2

本文深入探讨了AOP(面向切面编程)的核心概念,包括切入点表达式、通知方法和JoinPoint接口的使用。通过具体示例展示了如何在目标方法的不同阶段注入通知,如前置通知、后置通知、返回通知、异常通知和环绕通知。同时,文章还讲解了如何通过切入点表达式精准定位目标方法,以及如何获取和处理目标方法的详细信息。

一、连接的的细节

在这里插入图片描述
当前连接点(每个方法的每个位置)细节
1、概述
切入点表达式通常都会是从宏观上定位一组方法,和具体某个通知的注解结合起来就能够确定对应的连接点。那么就一个具体的连接点而言,我们可能会关心这个连接点的一些具体信息,例如:当前连接点所在方法的方法名、当前传入的参数值等等。这些信息都封装在JoinPoint接口的实例对象中。
2、JoinPoint
在这里插入图片描述


二、切入点表达式和切入点

/**
 * 如何将这个类(切面类)中的这些方法(通知方法)动态的在目标方法运行的各个位置切入
 * @author lfy
 *
 */
@Aspect
@Component
public class LogUtils {
	
	/**
	 * 告诉Spring每个方法都什么时候运行;
	 * try{
	 * 		@Before
	 * 		method.invoke(obj,args);
	 * 		@AfterReturning
	 * }catch(e){
	 * 		@AfterThrowing
	 * }finally{
	 * 		@After
	 * }
	 * 	
	 * 5个通知注解
	 * @Before:在目标方法之前运行;  					 前置通知
	 * @After:在目标方法结束之后						后置通知
	 * @AfterReturning:在目标方法正常返回之后			返回通知
	 * @AfterThrowing:在目标方法抛出异常之后运行			异常通知
	 * @Around:环绕								环绕通知
	 * 
	 */
	
	//想在执行目标方法之前运行;写切入点表达式
	//execution(访问权限符  返回值类型  方法签名)
	@Before("execution(public int com.atguigu.impl.MyMathCalculator.*(..))")
	public static void logStart(JoinPoint joinPoint){
		//获取到目标方法运行是使用的参数
		Object[] args = joinPoint.getArgs();
		//获取到方法签名,方法签名可比方法详细多了
		Signature signature = joinPoint.getSignature();
		String name = signature.getName();
		System.out.println("【"+name+"】方法开始执行,用的参数列表【"+Arrays.asList(args)+"】");
	}
	
	/**
	 * 切入点表达式的写法;
	 * 固定格式: execution(访问权限符  返回值类型  方法全类名(参数表))
	 *   
	 * 通配符:
	 * 		*:
	 * 			1)匹配一个或者多个字符:execution(public int com.atguigu.impl.MyMath*r.*(int, int))
	 * 			2)匹配任意一个参数:第一个是int类型,第二个参数任意类型;(匹配两个参数)
	 * 				execution(public int com.atguigu.impl.MyMath*.*(int, *))
	 * 			3)只能匹配一层路径
	 * 			4)权限位置*不能;权限位置不写就行,当然了不写也只能匹配public权限的;public【可选的】
	 * 		..:
	 * 			1)匹配任意多个参数,任意类型参数
	 * 			2)匹配任意多层路径:
	 * 				execution(public int com.atguigu..MyMath*.*(..));
	 * 
	 * 记住两种;
	 * 最精确的:execution(public int com.atguigu.impl.MyMathCalculator.add(int,int))
	 * 最模糊的:execution(* *.*(..)):千万别写;
	 * 
	 * &&”、“||”、“!
	 * 
	 * &&:我们要切入的位置满足这两个表达式
	 * 	MyMathCalculator.add(int,double)
	 * execution(public int com.atguigu..MyMath*.*(..))&&execution(* *.*(int,int))
	 * 
	 * 
	 * ||:满足任意一个表达式即可
	 * execution(public int com.atguigu..MyMath*.*(..))&&execution(* *.*(int,int))
	 * 
	 * !:只要不是这个位置都切入
	 * !execution(public int com.atguigu..MyMath*.*(..))
	 * 
	 * 告诉Spring这个result用来接收返回值:
	 * 	returning="result";
	 */
	//想在目标方法正常执行完成之后执行
	@AfterReturning(value="execution(public int com.atguigu..MyMath*.*(..))",returning="result")
	public static void logReturn(JoinPoint joinPoint,Object result){
		Signature signature = joinPoint.getSignature();
		String name = signature.getName();
		System.out.println("【"+name+"】方法正常执行完成,计算结果是:"+result);
	}

	
	/**
	 * 细节四:我们可以在通知方法运行的时候,拿到目标方法的详细信息;
	 * 1)只需要为通知方法的参数列表上写一个参数:
	 * 		JoinPoint joinPoint:封装了当前目标方法的详细信息
	 * 2)、告诉Spring哪个参数是用来接收异常
	 * 		throwing="exception":告诉Spring哪个参数是用来接收异常
	 * 3)、Exception exception:指定通知方法可以接收哪些异常,若范围小了,不能执行该通知方法
	 * 
	 * ajax接受服务器数据
	 * 	$.post(url,function(abc){
	 * 		alert(abc)
	 * 	})
	 */
	//想在目标方法出现异常的时候执行
	@AfterThrowing(value="execution(public int com.atguigu.impl.MyMathCalculator.*(..))",throwing="exception")
	public static void logException(JoinPoint joinPoint,Exception exception) {
		System.out.println("【"+joinPoint.getSignature().getName()+"】方法执行出现异常了,异常信息是【"+exception+"】:;这个异常已经通知测试小组进行排查");
	}

	//想在目标方法结束的时候执行
	/**
	 * Spring对通知方法的要求不严格;可以写private void 方法名都行
	 * 唯一要求的就是方法的参数列表一定不能乱写?
	 * 	通知方法是Spring利用反射调用的,每次方法调用得确定这个方法的参数表的值;
	 * 	参数表上的每一个参数,Spring都得知道是什么?
	 * 	JoinPoint:认识
	 * 	不知道的参数一定告诉Spring这是什么?
	 * 
	 * @param joinPoint
	 */
	@After("execution(public int com.atguigu.impl.MyMathCalculator.*(..))")
	private int logEnd(JoinPoint joinPoint) {
		System.out.println("【"+joinPoint.getSignature().getName()+"】方法最终结束了");
		return 0;
	}

}

三、通知方法的执行顺序

/**
 * 通知方法的执行顺序;
 * 
 *   try{
 *   	@Before
 *   	method.invoke(obj,args);
 *   	@AfterReturning
 *   }catch(){
 *   	@AfterThrowing
 *   }finally{
 *   	@After
 *   }
 * 
 * 正常执行:  @Before(前置通知)=====@After(后置通知)====@AfterReturning(正常返回);
 * 异常执行: @Before(前置通知)=====@After(后置通知)===@AfterThrowing(方法异常)
 * 
 */
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值