浅谈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


猜你喜欢
- 1、添加spring相关jar包2、配置ehcache jar包。3、添加ehcache mybatis 适配器jar包(在mybatis官
- 甲:听说最近java跌落神坛,python称霸武林了,你知道吗?乙:不是吧,我前几天看python怎么还是第三?丙:你们都在扯蛋,pytho
- Java语言的历程丰富多彩,被现在众多程序员和企业广泛使用,不用质疑这是Java的领先技术的结果。Java是Sun公司开发的一种编程语言,S
- 目录 一、Implicit和Explicit1、Implicit2、、Explicit先上一段奇怪的代码:if (dto.Paym
- 今天弄了一个多小时,写了一个GPS获取地理位置代码的小例子,包括参考了网上的一些代码,并且对代码进行了一些修改,希望对大家的帮助。具体代码如
- 最近,在与同事进行协同编程时,我们开始讨论在C#中初始化新对象的最佳方法。我一直是使用构造函数实现,尽管他倾向于静态工程方法。这引起了关于每
- ActiveMQ是什么ActiveMQ是消息队列技术,为解决高并发问题而生ActiveMQ生产者消费者模型(生产者和消费者可以跨平台、跨系统
- 自定义 webflux 容器配置配置代码@Componentpublic class ContainerConfig extends Rea
- 概述新版的音悦台 APP 播放页面交互非常有意思,可以把播放器往下拖动,然后在底部悬浮一个小框,还可以左右拖动,然后回弹的时候也会有相应的效
- 本文实例为大家解析了Zxing生成二维码的经典案例,供大家参考,具体内容如下1、首先呢,先编译 compile ‘com.google.zx
- 异步log4j2的location信息打印问题背景:项目改造过程中将log4j2改成异步,发现行号没有打印,于是扒了下官方文档,大概陈述下:
- Android 将view 转换为Bitmap出现空指针问题解决办法在做Android 项目的时候,有时候可能有这样的需求,将一个View
- 要“监听”事件,我们总是可以将“ * ”作为事件源中的另一个方法写入事件,但这将使事件源与 * 的逻辑紧密耦合。对于实际事件,我们比直接方法
- 使用maven的profile功能,我们可以实现多环境配置文件的动态切换,可参考我的上一篇博客。但随着SpringBoot项目越来越火,越来
- 代码如下所示: public static Bitmap
- 需求说明要求根据用户输入,生成相应组数的电话号码实现思路1、通过百度,获取对应真实世界中电话号码的头三位数2、采用Math.random()
- 本文实例讲述了WPF的ListView控件自定义布局用法。分享给大家供大家参考,具体如下:概要:以源码的形式贴出,免得忘记后,再到网上查资料
- 问题引出:最近开了新项目,项目中用到了数据字典,列表查询数据返回的时候需要手动将code转换为name,到前台展示。项目经理表示可以封装一个
- 简介netty中的数据是通过ByteBuf来进行传输的,一个ByteBuf中可能包含多个有意义的数据,这些数据可以被称作frame,也就是说
- 本文实例讲述了Spring Bean的初始化和销毁。分享给大家供大家参考,具体如下:一 点睛在开发过程中,经常遇到在Bean使用之前或者之后