前言
上一篇文章讲解了springboot aop 初步完整的使用和整合 这一篇讲解他的接口方法和类
JoinPoint和ProceedingJoinPoint对象
-
JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象. -
ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中
| 方法名 | 功能 |
|---|---|
| Signature getSignature(); | 获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息 |
| Object[] getArgs(); | 获取传入目标方法的参数对象 |
| Object getTarget(); | 获取被代理的对象 |
| Object getThis(); | 获取代理对象 |
1@Aspect 2@Component 3public class aopAspect { 4 /** 5 * 定义一个切入点表达式,用来确定哪些类需要代理 6 * execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理 7 */ 8 @Pointcut("execution(* aopdemo.*.*(..))") 9 public void declareJoinPointerExpression() {} 10 11 /** 12 * 前置方法,在目标方法执行前执行 13 * @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写 14 */ 15 @Before("declareJoinPointerExpression()") 16 public void beforeMethod(JoinPoint joinPoint){ 17 System.out.println("目标方法名为:" + joinPoint.getSignature().getName()); 18 System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); 19 System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName()); 20 System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); 21 //获取传入目标方法的参数 22 Object[] args = joinPoint.getArgs(); 23 for (int i = 0; i < args.length; i++) { 24 System.out.println("第" + (i+1) + "个参数为:" + args[i]); 25 } 26 System.out.println("被代理的对象:" + joinPoint.getTarget()); 27 System.out.println("代理对象自己:" + joinPoint.getThis()); 28 } 29 30 /** 31 * 环绕方法,可自定义目标方法执行的时机 32 * @param pjd JoinPoint的子接口,添加了 33 * Object proceed() throws Throwable 执行目标方法 34 * Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法 35 * 两个方法 36 * @return 此方法需要返回值,返回值视为目标方法的返回值 37 */ 38 @Around("declareJoinPointerExpression()") 39 public Object aroundMethod(ProceedingJoinPoint pjd){ 40 Object result = null; 41 42 try { 43 //前置通知 44 System.out.println("目标方法执行前..."); 45 //执行目标方法 46 //result = pjd.proeed(); 47 //用新的参数值执行目标方法 48 result = pjd.proceed(new Object[]{"newSpring","newAop"}); 49 //返回通知 50 System.out.println("目标方法返回结果后..."); 51 } catch (Throwable e) { 52 //异常通知 53 System.out.println("执行目标方法异常后..."); 54 throw new RuntimeException(e); 55 } 56 //后置通知 57 System.out.println("目标方法执行后..."); 58 59 return result; 60 } 61}
切点表达式
-
在Spring AOP中,连接点始终代表方法的执行。切入点是与连接点匹配的,切入点表达语言是以编程方式描述切入点的方式。
-
切入点(Poincut)是定义了在“什么地方”进行切入,哪些连接点会得到通知。显然,切点一定是连接点
-
切点是通过
@Pointcut注解和切点表达式定义的。@Pointcut注解可以在一个切面内定义可重用的切点。
execute表达式
*代表匹配任意修饰符及任意返回值,参数列表中..匹配任意数量的参数
可以使用&&、||、!、三种运算符来组合切点表达式,表示与或非的关系
- 拦截任意公共方法
execution(public * *(..)) - 拦截以set开头的任意方法
execution(* set*(..)) - 拦截类或者接口中的方法
1拦截AccountService(类、接口)中定义的所有方法 2execution(* com.xyz.service.AccountService.*(..))
- 拦截包中定义的方法,不包含子包中的方法
1拦截com.xyz.service包中所有类中任意方法,**不包含**子包中的类 2execution(* com.xyz.service.*.*(..))
- 拦截包或者子包中定义的方法
1拦截com.xyz.service包或者子包中定义的所有方法 2execution(* com.xyz.service..*.*(..))
通知分类
@Before
- 前置通知: 在方法执行之前执行
- 前置通知使用
@Before注解 将切入点表达式值作为注解的值

@After
- 后置通知, 在方法执行之后执行
- 后置通知使用
@After注解 ,在后置通知中,不能访问目标方法执行的结果

@AfterRunning
- 返回通知, 在方法返回结果之后执行
- 返回通知使用
@AfterRunning注解


@AfterThrowing
- 异常通知, 在方法抛出异常之后执行
- 异常通知使用
@AfterThrowing注解

@Around
- 环绕通知, 围绕着方法执行
- 环绕通知使用
@Around注解

1package com.jason.spring.aop.impl; 2 3import java.util.Arrays; 4import java.util.List; 5 6import org.aspectj.lang.JoinPoint; 7import org.aspectj.lang.ProceedingJoinPoint; 8import org.aspectj.lang.annotation.After; 9import org.aspectj.lang.annotation.AfterReturning; 10import org.aspectj.lang.annotation.AfterThrowing; 11import org.aspectj.lang.annotation.Around; 12import org.aspectj.lang.annotation.Aspect; 13import org.aspectj.lang.annotation.Before; 14import org.springframework.stereotype.Component; 15 16 17//把这个类声明为一个切面 18//1.需要将该类放入到IOC 容器中 19@Component 20//2.再声明为一个切面 21@Aspect 22public class LoggingAspect { 23 24 //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法 25 //作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法 26 //支持通配符 27 //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))") 28 @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))") 29 public void beforeMethod(JoinPoint joinPoint){ 30 String methodName = joinPoint.getSignature().getName(); 31 List<Object> args = Arrays.asList(joinPoint.getArgs()); 32 System.out.println("The method " + methodName + " begins " + args); 33 } 34 35 /** 36 * @Description: 在方法执行后执行的代码,无论该方法是否出现异常 37 * @param joinPoint 38 */ 39 @After("execution(* com.jason.spring.aop.impl.*.*(int, int))") 40 public void afterMethod(JoinPoint joinPoint){ 41 String methodName = joinPoint.getSignature().getName(); 42 List<Object> args = Arrays.asList(joinPoint.getArgs()); 43 System.out.println("The method " + methodName + " end " + args); 44 } 45 46 /** 47 * 48 * @Description: 在方法正常结束后执行代码,放回通知是可以访问到方法的返回值 49 * 50 * @param joinPoint 51 */ 52 @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result") 53 public void afterReturning(JoinPoint joinPoint ,Object result){ 54 String methodName = joinPoint.getSignature().getName(); 55 System.out.println("The method " + methodName + " end with " + result); 56 } 57 58 /** 59 * 60 * @Description: 在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码 61 * 62 * @param joinPoint 63 * @param ex 64 */ 65 @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex") 66 public void afterThrowting(JoinPoint joinPoint, Exception ex){ 67 String methodName = joinPoint.getSignature().getName(); 68 System.out.println("The method " + methodName + " occurs exceptions " + ex); 69 } 70 71 /** 72 * 73 * @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数 74 * 环绕通知 类似于 动态代理的全过程 75 * ProceedingJoinPoint:可以决定是否执行目标方法 76 * 环绕通知必须有返回值,返回值即为目标方法的返回值 77 * 78 * @param proceedingJoinPoint 79 */ 80 @Around("execution(* com.jason.spring.aop.impl.*.*(..))") 81 public Object around(ProceedingJoinPoint proceedingJoinPoint){ 82 83 Object result = null; 84 String methodName = proceedingJoinPoint.getSignature().getName(); 85 86 //执行目标方法 87 try { 88 //前置通知 89 System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs())); 90 91 result = proceedingJoinPoint.proceed(); 92 93 //后置通知 94 System.out.println("The method " + methodName + "end with" + result); 95 96 } catch (Throwable e) { 97 //异常通知 98 System.out.println("The method occurs exception : " + e); 99 throw new RuntimeException(); 100 } 101 //后置通知 102 103 System.out.println("The method " + methodName + "end with" + result); 104 105 return result; 106 } 107}
