经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringAOP如何获取方法参数上的注解
来源:jb51  时间:2021/8/9 10:21:49  对本文有异议

SpringAOP获取方法参数上的注解

一、示例

① 如下代码,自定义一个参数注解@Test,并将其使用到方法参数上,用于标注需要检验的参数

  1. /**
  2. * 自定义注解,用于参数
  3. */
  4. @Target(PARAMETER)
  5. @Documented
  6. @Retention(RetentionPolicy.RUNTIME)
  7. public @interface Test{
  8. }
  9. /**
  10. * 接口层,使用使用@Test注解标记参数
  11. */
  12. @RestController
  13. @RequestMapping("/v1/test")
  14. public class TestController {
  15. @PostMapping(value = "/email", produces = "application/json")
  16. public String send(@RequestBody @Test MailSendDTO mailSendDTO) {
  17. //TODO 业务处理
  18. return "SUCCESS";
  19. }
  20. }

② 通过切面拦截该方法,从连接点获取signature,并将signature强转为MethodSignature,从而从MethodSignature对象可以获取拦截的方法对象以及方法参数注解

  1. @Aspect
  2. @Configuration
  3. public class ValidateAop {
  4. /**
  5. * 切点配置,表达式, 在com.laoxi.test.controller包下,所有的类public的任意参数的方法
  6. */
  7. @Pointcut("execution(public * com.laoxi.test.controller.*.*(..))")
  8. public void validate(){}
  9. @Before("validate()")
  10. public void doBefore(JoinPoint joinPoint){
  11. Object[] params = joinPoint.getArgs();
  12. if(params.length == 0){
  13. return;
  14. }
  15. //获取方法,此处可将signature强转为MethodSignature
  16. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  17. Method method = signature.getMethod();
  18. //参数注解,1维是参数,2维是注解
  19. Annotation[][] annotations = method.getParameterAnnotations();
  20. for (int i = 0; i < annotations.length; i++) {
  21. Object param = params[i];
  22. Annotation[] paramAnn = annotations[i];
  23. //参数为空,直接下一个参数
  24. if(param == null || paramAnn.length == 0){
  25. continue;
  26. }
  27. for (Annotation annotation : paramAnn) {
  28. //这里判断当前注解是否为Test.class
  29. if(annotation.annotationType().equals(Test.class)){
  30. //校验该参数,验证一次退出该注解
  31. //TODO 校验参数
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. }

二、debug

通过debug代码:

可发现连接点实际为MethodInvocationProceedingJoinPoint对象,连接点中的signature则为MethodSignatureImpl对象,是MethodInvocationProceedingJoinPoint的内部类。

三、类图及源码

MethodInvocationProceedingJoinPoint类图,顶级实现了JoinPoint(以后再使用切面的时候,可以看看其他类里面都扩展了哪些方法可以直接使用)

MethodSignatureImpl类图,顶级实现了Signature(以后再使用切面的时候,可以看看其他类里面都扩展了哪些方法可以直接使用)

用AOP拦截自定义注解并获取注解属性与上下文参数(基于Springboot框架)

AOP可以用于日志的设计,这样话就少不了要获取上下文的信息,博主在设计日志模块时考虑了一下此法,整理了一下如何用AOP来拦截你自定义的注解。

自定义注解

首先先自定义一个注解

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface Axin {
  5. /**
  6. * 所属模块
  7. * @return
  8. */
  9. String module() default "日志模块";
  10. /**
  11. * 动作描述
  12. * @return
  13. */
  14. String desc() default "无动作";
  15. }

@Documented:注解表明制作javadoc时,是否将注解信息加入文档。如果注解在声明时使用了@Documented,则在制作javadoc时注解信息会加入javadoc。

@Target:用来说明该注解可以被声明在那些元素之前

  • @Target(ElementType.TYPE) //接口、类、枚举、注解
  • @Target(ElementType.FIELD) //字段、枚举的常量
  • @Target(ElementType.METHOD) //方法
  • @Target(ElementType.PARAMETER) //方法参数
  • @Target(ElementType.CONSTRUCTOR) //构造函数
  • @Target(ElementType.LOCAL_VARIABLE)//局部变量
  • @Target(ElementType.ANNOTATION_TYPE)//注解
  • @Target(ElementType.PACKAGE) ///包

@Retention:用来说明该注解类的生命周期。

  • @Retention(RetentionPolicy.SOURCE) —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略
  • @Retention(RetentionPolicy.CLASS) —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略
  • @Retention(RetentionPolicy.RUNTIME) —— 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用.

定义切面

  1. /**
  2. * @author Axin
  3. */
  4. @Aspect
  5. @Component
  6. public class AxinAspect {
  7. /**
  8. * 这里定义了一个总的匹配规则,以后拦截的时候直接拦截log()方法即可,无须去重复写execution表达式
  9. */
  10. @Pointcut("@annotation(Axin)")
  11. public void log() {
  12. }
  13. @Before("log()&&@annotation(axin)")
  14. public void doBefore(JoinPoint joinPoint,Axin axin) {
  15. System.out.println("******拦截前的逻辑******");
  16. System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
  17. System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());
  18. System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
  19. System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
  20. //获取传入目标方法的参数
  21. Object[] args = joinPoint.getArgs();
  22. for (int i = 0; i < args.length; i++) {
  23. System.out.println("第" + (i + 1) + "个参数为:" + args[i]);
  24. }
  25. System.out.println("被代理的对象:" + joinPoint.getTarget());
  26. System.out.println("代理对象自己:" + joinPoint.getThis());
  27. System.out.println("拦截的注解的参数:");
  28. System.out.println(axin.module());
  29. System.out.println(axin.desc());
  30. }
  31. @Around("log()&&@annotation(axin)")
  32. public Object doAround(ProceedingJoinPoint proceedingJoinPoint,Axin axin) throws Throwable {
  33. System.out.println("环绕通知:");
  34. System.out.println(axin.module());
  35. System.out.println(axin.desc());
  36. Object result = null;
  37. result = proceedingJoinPoint.proceed();
  38. return result;
  39. }
  40. @After("log()")
  41. public void doAfter() {
  42. System.out.println("******拦截后的逻辑******");
  43. }
  44. }

匹配规则:

  1. //匹配AOP对象的目标对象为指定类型的方法,即DemoDao的aop的代理对象
  2. @Pointcut("this(com.hhu.DemaoDao)")
  3. public void thisDemo() {
  4. ...
  5. }

通知类别:

前置通知(Before advice)- 在目标方便调用前执行通知

后置通知(After advice)- 在目标方法完成后执行通知

返回通知(After returning advice)- 在目标方法执行成功后,调用通知

异常通知(After throwing advice)- 在目标方法抛出异常后,执行通知

环绕通知(Around advice)- 在目标方法调用前后均可执行自定义逻辑

获取上下文信息JoinPoint

JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象. 注意:这用于非环绕通知

方法名 功能
Signature getSignature(); 获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
Object[] getArgs(); 获取传入目标方法的参数对象
Object getTarget(); 获取被代理的对象
Object getThis(); 获取代理对象

方法使用模板:

  1. public void doBefore(JoinPoint joinPoint) {
  2. System.out.println("******拦截前的逻辑******");
  3. System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
  4. System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());
  5. System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
  6. System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
  7. //获取传入目标方法的参数
  8. Object[] args = joinPoint.getArgs();
  9. for (int i = 0; i < args.length; i++) {
  10. System.out.println("第" + (i + 1) + "个参数为:" + args[i]);
  11. }
  12. System.out.println("被代理的对象:" + joinPoint.getTarget());
  13. System.out.println("代理对象自己:" + joinPoint.getThis());
  14. }

ProceedingJoinPoint

ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中

方法名 功能
Object proceed() throws Throwable 执行目标方法
Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法

定义测试方法

  1. //Service接口
  2. public interface AxinService {
  3. String axinRun(String arg1, User user);
  4. }
  5. //实现类
  6. /**
  7. * @author Axin
  8. */
  9. @Component
  10. public class AxinServiceImpl implements AxinService {
  11. @Axin(module = "print",desc = "打印")
  12. @Override
  13. public String axinRun(String arg1, User user) {
  14. String res = arg1 + user.getName() + user.getAge();
  15. return res;
  16. }
  17. public String axinRun(String arg1, Person person) {
  18. String res = arg1 + person.getName() + person.getAge();
  19. return res;
  20. }
  21. }
  22. //控制类
  23. /**
  24. * @author Axin
  25. */
  26. @RestController
  27. public class HelloController {
  28. @Autowired
  29. AxinService axinService;
  30. @RequestMapping("/hello")
  31. public String hello() {
  32. User user = new User();
  33. user.setAge(10);
  34. user.setName("张三");
  35. String res = axinService.axinRun("Test:", user);
  36. return "Hello Spring Boot!<br>"+res;
  37. }
  38. }

测试结果

环绕通知:
print
打印
******拦截前的逻辑******
目标方法名为:axinRun
目标方法所属类的简单类名:AxinService
目标方法所属类的类名:com.axin.springboot.service.AxinService
目标方法声明类型:public abstract
第1个参数为:Test:
第2个参数为:User(id=null, name=张三, age=10, date=null)
被代理的对象:com.axin.springboot.service.AxinServiceImpl@ac2ddcc
代理对象自己:com.axin.springboot.service.AxinServiceImpl@ac2ddcc
拦截的注解的参数:
print
打印
******拦截后的逻辑******

小结

通过上述的代码演示,我们可以自定义一个注解,然后配置切面来拦截有注解的方法,同时也可以获得方法传入的参数来完成你的业务需求。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号