springboot注解式AOP通过JoinPoint获取参数学习笔记springboot注解式AOP通过JoinPoint获取参数学习笔记
之前开发时,需要获取切点注解的参数值,记录⼀下
切⾯注解 :
@Aspect – 标识为⼀个切⾯供容器读取,作⽤于类
@Pointcut – (切⼊点):就是带有通知的连接点
@Before – 前置
@AfterThrowing – 异常抛出
@After – 后置
@AfterReturning – 后置增强,执⾏顺序在@After之后
@Around – 环绕
1.相关maven包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.⾃定义⼀个注解
import*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public@interface Action {
String value()default"list";
}
3.定义切⾯类
import JoinPoint;
import AfterReturning;
import Aspect;
import MethodSignature;
import Component;
import Method;
import ArrayList;
import Collection;
import List;
@Aspect
@Component
public class ActAspect {
@AfterReturning("@annotation(包名.Action)")
public void afterReturning(JoinPoint point){
// 获取切⼊点⽅法名
String methodName = Signature().getName();
springboot aop
// 获取注解中的参数值
MethodSignature methodSignature =(Signature();        Method method = Method();
// 获取注解Action
Action annotation = Annotation(Action.class);
// 获取注解Action的value参数的值
String value = annotation.value();
// 获取切点⽅法⼊参列表
Object[] objArray = Args();
// 下⾯代码根据具体⼊参类型进⾏修改
List<String> list =new ArrayList<>();
for(Object obj: objArray){
if(obj instanceof Collection){
list =(List<String>) obj;
}
}
}
}
参考博客:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。