AOP中ProceedingJoinPoint获取目标方法,参数,注解
private void saveLog(ProceedingJoinPoint jp,long time)throws Throwable {
package com.cy.pj.common.aspect;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.cy.pj.common.annotation.RequiredLog;
import com.cy.pj.common.util.IPUtils;
import com.cy.pj.sys.entity.SysLog;
import com.cy.pj.sys.service.SysLogService; import lombok.extern.slf4j.Slf4j;
/**
* @Aspect 描述的类为切面类,此类中实现:
* 1)切入点(Pointcut)的定义
* 2)通知(advice)的定义(扩展功能)
*/
@Slf4j
@Aspect
@Component
public class SysLogAspect {
/**
* @Pointcut 注解用于描述或定义一个切入点
* 切入点的定义需要遵循spring中指定的表达式规范
例如:("bean(sysMenuServiceImpl)")为切入点表达式
的一种定义方式。
*/
//bean(bean名称或一个表达式)
//@Pointcut("bean(sysMenuServiceImpl)")
@Pointcut("@annotation(com.cy.pj.common.annotation.RequiredLog)")
public void logPointCut() {} /**
* @Around 注解描述的方法为一个环绕通知方法,
* 在此方法中可以添加扩展业务逻辑,可以调用下一个
切面对象或目标方法
* @param jp 连接点(此连接点只应用@Around描述的方法)
* @return
* @throws Throwable
*/
@Around("logPointCut()")
public Object aroundAdvice(ProceedingJoinPoint jp)
throws Throwable{
long start=System.currentTimeMillis();
log.info("start:"+start);
Object result=jp.proceed();//调用下一个切面或目标方法
long end=System.currentTimeMillis();
log.info("end:"+end);
//记录日志(用户行为信息)
saveLog(jp,(end-start));
return result;
}
@Autowired
private SysLogService sysLogService;
//日志记录
private void saveLog(ProceedingJoinPoint jp,long time)throws Throwable {
//1.获取用户行为日志(ip,username,operation,method,params,time,createdTime)
//获取类的字节码对象,通过字节码对象获取方法信息
Class<?> targetCls=jp.getTarget().getClass();
//获取方法签名(通过此签名获取目标方法信息)
MethodSignature ms=(MethodSignature)jp.getSignature();
//获取目标方法上的注解指定的操作名称
Method targetMethod=
targetCls.getDeclaredMethod(
ms.getName(),
ms.getParameterTypes());
RequiredLog requiredLog=
targetMethod.getAnnotation(RequiredLog.class);
String operation=requiredLog.value();
System.out.println("targetMethod="+targetMethod);
//获取目标方法名(目标类型+方法名)
String targetClsName=targetCls.getName();
String targetObjectMethodName=targetClsName+"."+ms.getName();
//获取请求参数
String targetMethodParams=Arrays.toString(jp.getArgs());
//2.封装用户行为日志(SysLog)
SysLog entity=new SysLog();
entity.setIp(IPUtils.getIpAddr());
entity.setUsername("admin");
entity.setOperation(operation);
entity.setMethod(targetObjectMethodName);
entity.setParams(targetMethodParams);
entity.setTime(time);
entity.setCreatedTime(new Date());
//3.调用业务层对象方法(saveObject)将日志写入到数据库
sysLogService.saveObject(entity);
}
}
}
https://www.yuque.com/binarylei/java/annotation
AOP中ProceedingJoinPoint获取目标方法,参数,注解的更多相关文章
- 学习spring第6天(aop获取目标方法参数)
关于<aop:around>中的方法,需要第一个参数为ProceedJoinPoint,在方法体中通过该参数调用proceed()才能使目标方法得到调用. 当一个切面中有多个<aop ...
- 参数上使用自定义注解在aop中无法获取到该参数
https://ask.csdn.net/questions/769477 /** * 环绕增强,验证权限 * @param joinPoint 目标对象 * @param authCheck 自定义 ...
- AOP实现拦截对象以及获取切入目标方法和注解
AOP实现拦截对象以及获取切入目标方法和注解 一.JoinPoint是什么? AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用 o ...
- Spring:使用Spring AOP时,如何获取目标方法上的注解
当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等 自定义注解 package com.agent.annotation; import java.lang.annot ...
- Spring Aop 修改目标方法参数和返回值
一.新建注解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Document ...
- Eclipse中自动提示的方法参数都是arg0,arg1的解决方法
Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...
- 为何Spring MVC可获取到方法参数名,而MyBatis却不行?【享学Spring MVC】
每篇一句 胡适:多谈些问题,少聊些主义 前言 Spring MVC和MyBatis作为当下最为流行的两个框架,大家平时开发中都在用.如果你往深了一步去思考,你应该会有这样的疑问: 在使用Spring ...
- java反射之获取所有方法及其注解(包括实现的接口上的注解),获取各种标识符备忘
java反射之获取类或接口上的所有方法及其注解(包括实现的接口上的注解) /** * 获取类或接口上的所有方法及方法上的注解(包括方法实现上的注解以及接口上的注解),最完整的工具类,没有现成的工具类 ...
- Freemarker中Configuration的setClassForTemplateLoading方法参数问题
今天使用freemarker中Configuration的setClassForTemplateLoading方法遇到了加载模板目录的一个小问题. 由于网上的其他论坛,博客写的有点乱,故记录一下. F ...
随机推荐
- Oracle 相关命令
http://www.mamicode.com/info-detail-2481866.html sql语句 system用户登陆 查看表空间和存放位置 select t1.name,t2.name ...
- [源码解析] PyTorch 如何使用GPU
[源码解析] PyTorch 如何使用GPU 目录 [源码解析] PyTorch 如何使用GPU 0x00 摘要 0x01 问题 0x02 移动模型到GPU 2.1 cuda 操作 2.2 Modul ...
- TestNG 参数化应用
一.第一种参数化方式(testng.xml配置参数) 1.新建ParameterDemo04类 2.拷贝类的路径 3.testng.xml配置类地址及参数 <?xml version=" ...
- Screenshot 库和Collections 库
一.screenShot 是 robot framework的标准类库,用于截取当前窗口,需要手动加载. 示例: 运行结果: 二.Collections 库 Collections 库同样为 Robo ...
- go 集合
p.p1 { margin: 0; font: 12px ".PingFang SC"; color: rgba(69, 69, 69, 1) } span.s1 { font: ...
- Django 小实例S1 简易学生选课管理系统 7 修改个人信息
Django 小实例S1 简易学生选课管理系统 第7节--修改个人信息 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 用户模块除了注册登录之外,还 ...
- PLSQL 删表 恢复
1.查看你删除的是哪张表(SQL 中的时间是删表时的时间 (我删表的时间 大概是:2019-08-16 08:47:00 之后 )): select * from user_recy ...
- kubernetes基本概念 pod, service
k8s的部署架构 kubernetes中有两类资源,分别是master和nodes,master和nodes上跑的服务如下图, kube-apiserver | kubelet kube-contro ...
- MySQL 1064 错误
ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL s ...
- SpringCloud升级之路2020.0.x版-40. spock 单元测试封装的 WebClient(上)
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们来测试下前面封装好的 WebClient,这里开始,我们使用 spock 编写 gro ...