浅谈springboot之JoinPoint的getSignature方法
作者:Mint6 时间:2022-12-25 11:23:20
JoinPoint的getSignature方法
在使用springboot写aop的时候,有个JoinPoint类,用来获取代理类和被代理类的信息。
这个文章记录一下JoinPoint的getSignature方法返回的是什么格式。
不废话,贴代码
package org.aspectj.lang;
public interface Signature {
String toString();
String toShortString();
String toLongString();
String getName();
int getModifiers();
Class getDeclaringType();
String getDeclaringTypeName();
}
打印输出,getString是测试类的方法名,TestController是类名
joinPoint.getSignature().toString():String com.fast.web.controller.TestController.getString()
joinPoint.getSignature().toShortString():TestController.getString()
joinPoint.getSignature().toLongString():public java.lang.String com.fast.web.controller.TestController.getString()
joinPoint.getSignature().getName():getString
joinPoint.getSignature().getModifiers():1
joinPoint.getSignature().getDeclaringType():class com.fast.web.controller.TestController
joinPoint.getSignature().getDeclaringTypeName():com.fast.web.controller.TestController
冒号前面是使用的方法,后面是本次测试输出的结果。
附上被测试的类:
package com.fast.web.controller;
import com.fast.framework.dao.TestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestDao testDao;
@RequestMapping("/test")
public String getString() {
int i = testDao.selectBase();
return String.valueOf(i);
}
}
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 java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
String value() default "list";
}
3.定义切面类
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Aspect
@Component
public class ActAspect {
@AfterReturning("@annotation(包名.Action)")
public void afterReturning(JoinPoint point){
// 获取切入点方法名
String methodName = point.getSignature().getName();
// 获取注解中的参数值
MethodSignature methodSignature = (MethodSignature)point.getSignature();
Method method = methodSignature.getMethod();
// 获取注解Action
Action annotation = method.getAnnotation(Action.class);
// 获取注解Action的value参数的值
String value = annotation.value();
// 获取切点方法入参列表
Object[] objArray = point.getArgs();
// 下面代码根据具体入参类型进行修改
List<String> list = new ArrayList<>();
for (Object obj: objArray) {
if(obj instanceof Collection){
list = (List<String>) obj;
}
}
}
}
来源:https://blog.csdn.net/Mint6/article/details/92121845
标签:springboot,JoinPoint,getSignature
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android学习之BottomSheetDialog组件的使用
2023-12-10 07:58:30
![](https://img.aspxhome.com/file/2023/4/137754_0s.jpg)
Android RecyclerView的简单使用
2022-08-07 22:04:23
![](https://img.aspxhome.com/file/2023/2/96022_0s.jpg)
Flutter混合开发详解
2023-05-15 11:10:54
![](https://img.aspxhome.com/file/2023/6/137686_0s.png)
Android利用二阶贝塞尔曲线实现添加购物车动画详解
2022-03-01 09:49:49
![](https://img.aspxhome.com/file/2023/0/138540_0s.gif)
浅谈SpringMVC的执行流程
2023-09-30 17:59:59
![](https://img.aspxhome.com/file/2023/8/62358_0s.png)
Android实现毛玻璃效果的对话框
2021-07-19 03:28:42
![](https://img.aspxhome.com/file/2023/8/94258_0s.png)
java实现支付宝退款功能
2021-09-25 14:00:00
![](https://img.aspxhome.com/file/2023/0/62240_0s.jpg)
C#实现的微信网页授权操作逻辑封装示例
2022-08-31 04:30:05
解析java基本数据类型传递与引用传递区别
2022-04-14 18:37:19
![](https://img.aspxhome.com/file/2023/8/113458_0s.png)
Android 支付宝支付、微信支付、银联支付 整合第三方支付接入方法(后台订单支付API设计)
2023-08-28 01:36:01
![](https://img.aspxhome.com/file/2023/0/139530_0s.png)
C#图像伪彩色处理方法
2022-09-23 10:51:52
![](https://img.aspxhome.com/file/2023/1/92911_0s.jpg)
Ubuntu中为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
2022-07-20 15:09:51
浅谈java面向对象中四种权限
2023-03-09 18:43:46
c#获取本机的IP地址的代码
2021-08-21 08:37:07
java 集合----Map、Collection
2022-11-09 03:39:01
C#异步编程由浅入深(一)
2023-12-16 07:32:29
C# PictureBox图片控件实现图片交换
2022-12-05 17:09:10
![](https://img.aspxhome.com/file/2023/5/126025_0s.png)
Android超详细讲解组件ScrollView的使用
2022-08-05 09:55:18
![](https://img.aspxhome.com/file/2023/9/94989_0s.png)
Java毕业设计实战之仿小米电子产品售卖商城系统的实现
2022-09-29 10:19:47
![](https://img.aspxhome.com/file/2023/0/72520_0s.jpg)
java异常处理的简单练习
2023-12-01 02:20:48