Spring Aop 如何获取参数名参数值
作者:小动物不困i 发布时间:2022-09-08 17:00:41
标签:Spring,Aop,参数名,参数值
前言:
有时候我们在用Spring Aop面向切面编程,需要获取连接点(JoinPoint)方法参数名、参数值。
环境:
Mac OSX
Intellij IDEA
Spring Boot 2x
Jdk 1.8x
Code:
package com.example.aopdemo.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* DemoAop
* Create by Gray(Ganguocai@outlook.com)
*/
@Aspect
@Component
@Slf4j
public class DemoAop {
/**
* 环绕通知
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around(value = "execution(* com.example.aopdemo..*(..)))")
public Object demoAop(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.debug("执行前:");
Map<String, Object> params = getNameAndValue(proceedingJoinPoint);
for (Map.Entry<String, Object> entry : params.entrySet()) {
System.out.println("name: " + entry.getKey() + " value: " + entry.getValue());
}
Object object = proceedingJoinPoint.proceed(); //执行连接点方法,object:方法返回值
log.debug("执行后:");
return object;
}
/**
* 获取参数Map集合
* @param joinPoint
* @return
*/
Map<String, Object> getNameAndValue(ProceedingJoinPoint joinPoint) {
Map<String, Object> param = new HashMap<>();
Object[] paramValues = joinPoint.getArgs();
String[] paramNames = ((CodeSignature)joinPoint.getSignature()).getParameterNames();
for (int i = 0; i < paramNames.length; i++) {
param.put(paramNames[i], paramValues[i]);
}
return param;
}
}
AOP切面获取参数的一个小技巧
一般来说,我们的参数,都是通过json传递的,那么这个问题就转化成了,从json中获取指定字符串的问题。
OK,这个问题就简单了。
如下:
public static void main(String[] args) {
// 这里JSONObject是fastjson-1.2.41.jar包下的
JSONObject jsonObject = JSON.parseObject("{\"timeStamp\":21602756894612,\"status\":0,\"results\":{\"userName\":\"yang20102\",\"userLevel\":\"3\"},\"errorCode\":null,\"errorMessage\":null}");
// 获取json最外层字符串
Object timeStamp = jsonObject.get("timeStamp");
System.out.println("timeStamp:" + timeStamp);
// 获取复杂对象
Object results = jsonObject.get("results");
JSONObject jsonObjectResults = JSON.parseObject(results.toString());
Object userName = jsonObjectResults.get("userName");
System.out.println("userName:" + userName);
}
实例json如下:
{
"timeStamp": 21602756894612,
"status": 0,
"results": {
"userName": "yang20102",
"userLevel": "3"
},
"errorCode": null,
"errorMessage": null
}
来源:https://blog.csdn.net/weixin_39931896/article/details/83451311
0
投稿
猜你喜欢
- 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;
- 所以对于应用层用着还不是很方便。最近做一个项目顺便就封装了一个调用默认打印机的类。虽说有几个小bug,但对于目前来说,已经满足需求了。以后不
- 本文实例讲述了C#实现在启动目录创建快捷方式的方法。分享给大家供大家参考。具体如下:添加引用,选择 COM 选项卡并选择 Windows S
- 简单工厂模式介绍:概要:简单工厂模式,又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模
- 什么是Junit5 ?先看来个公式:JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vin
- 这篇文章主要介绍了简单了解java标识符的作用和命名规则,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 1: .net framework 由两个部分组成:CLR 和 FCL。2:在CLR中,所有错误都是通过异常来报告的。3:智能感知功能主要是
- 具体代码如下所示:public class Student { private String id; private
- 之前在项目中会用到在Java在后台把数据填入Word文档的模板来提供前台下载,为了自己能随时查看当时的实现方案及方便他人学习我写了这篇博客,
- 最近正在学习c#,对其中的方法重写和隐藏的概念很是模糊,现在将其归纳如下:1:方法重写:就是在基类中的方法用virtual关键字来标识,然后
- JPA是什么? JPA(Java Persistence API)是Sun官方提出的Java持久化规范. 为Java开发人员提供了一种对象/
- mapper.xml使用循环语句mapper.java,传的参数是mapList<实体类> getList(Map<Str
- 在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来
- 1.如何创建一个有效的AudioRecorder实例 Android各种设备的采样频率不同,输入的声道数也不同,如果采用固定的采样频率和声道
- 前言相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷。我们在Spring Boot使
- 1.C++中的时间:(1) time_t其实是一个64位的long int类型(2) time函数:函数简介:函数名: time
- 前言这周接到一个需求,需要在应用从后台切换到前台时,展示我们的广告。展示页面其实可以复用以前的开屏广告页,唯一的问题就是如何监听应用从后台切
- 在Struts2中Action部分,也就是Controller层采用了低侵入的方式。为什么这么说?这是因为在Struts2中action类并
- 递归是一个非常有用的知识点。写点实例帮助自己记忆中间有过程代码首先一个javapojo类package com.qcf.po;import
- 前言内存管理的目的就是让我们在开发过程中有效避免我们的应用程序出现内存泄露的问题。内存泄露相信大家都不陌生,我们可以这样理解:「没有用的对象