浅谈springboot之JoinPoint的getSignature方法
作者:Mint6 发布时间:2022-12-25 11:23:20
标签:springboot,JoinPoint,getSignature
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
0
投稿
猜你喜欢
- 最近在做项目的时候有用到对两个集合中的元素进行对比求其交集的情况,因为涉及到的数据量比较大,所以在进行求两个集合中元素交集的时候,就应该考虑
- 在spring中有很多以XXXAware命名的接口,很多人也不清楚这些接口都是做什么用的,这篇文章将描述常用的一些接口。一,Applicat
- 首先当我们将Dwr3配置好以后,我们可以在浏览器中测试一下,查看一下我们配置的Dwr有没有生效,方法是http://localhost:[你
- 本文分享的示例代码实现提取2个集合中相同和不同的元素此处需要使用Collection集合所提供的一个方法:removeAll(Cellect
- 前言最近有项目需要开发档案打包下载功能,其中包含很多大附件,项目使用minio存储且不在同一台服务器上,为了优化速度决定使用windows共
- 一、介绍在实际的软件项目开发过程中,我可以很负责任的跟大家说,如果你真的实际写代码的时间超过5年,你对增删改查这类简单的功能需求开发,可以说
- 无论哪种界面框架输入文本框都是非常重要的控件, 但是发现flutter中的输入框TextField介绍的虽然多,但是各个属性怎么组合满足需要
- Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式: public static
- 前言本文给你提供在Spring Boot 应用程序中编写好的单元测试的机制,并且深入技术细节。我们将带你学习如何以可测试的方式创建Sprin
- 条件:1、android:ellipsize=”marquee”2、TextView必须单行显示,即内容必须超出TextView
- 前言前几天恰好面试一个应届生,问了一个很简单的问题:你了解过Mybatis中有几种传参方式吗?没想到其他问题回答的很好,唯独这个问题一知半解
- 目前很多业务使用微服务架构,服务模块划分有这2种方式:服务功能划分业务划分不管哪种方式,一次接口调用都需要多个服务协同完成,其中一个服务出现
- 解决方法有如下两种:第一种如果你 repo sync 了 android 的整个源码,那么可以直接把你的 app 放到 /packages/
- 1、Service层:业务层–>控制业务业务模块的逻辑功能设计,和DAO层一样都是先设计接口,再创建要实现的类,然
- 最近碰到这么个恶心的问题问题:有个arr文件被放到Module A中引用,现在Module B又依赖了Module A,则在编译过程中会发生
- 背景最近好几个项目在运行过程中客户都提出文件上传大小的限制能否设置的大一些,用户经常需要上传好几个G的资料文件,如图纸,视频等,并且需要在上
- 一、打印直角三角形这个循环控制打印十行空格for (int x = 1; x <= 10; x++) {//因为要打印一个十行的直角三
- 前言我们在日常开发中,经常会用到一个系统需要链接多个数据库来实现业务的需求,比如多个系统之间数据调用、两个数据之间同步等等。今天给大家分享使
- Android 自动获取验证码的两种方式分别是BroadcastReceiver及ContentObserver,两种方式都需要进行注册、取
- 目录题目及要求:提示:原创代码:代码思路:题目及要求:给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。提示:0 <