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 ...
随机推荐
- 网关服务spring cloud zuul
Zuul例子配置文件 spring.application.name=switch-gateway server.port=5555 请求路由 传统路由方式 zuul.routes.api-a-url ...
- SpringBoot 居然有 44 种应用启动器
啥是应用启动器?SpringBoot集成了spring的很多模块,比如tomcat.redis等等.你用SpringBoot搭建项目,只需要在pom.xml引入相关的依赖,和在配置文件中简单的配置就可 ...
- c++学习笔记5(函数的缺省参数)
例: void func(int x1,int x2=2,int x3=3){} func (10)//等效于func (10,2,3) func (10,8)//等效于func (10,8,3) f ...
- MySQL基础语句(MySQL内置函数 )
MySQL 字符串函数 函数 描述 实例 ASCII(s) 返回字符串 s 的第一个字符的 ASCII 码. 返回 CustomerName 字段第一个字母的 ASCII 码: SELECT ASCI ...
- go微服务框架Kratos笔记(七)使用jwt认证中间件
引言 Json web token (JWT) 是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,特别适用于分布式站点的单点登录(SSO)场景.JWT的声明一般被用来在身份提供者和 ...
- 菜鸡的Java笔记 第二十三 - java 抽象类的概念
abstractClass 抽象类的概念 1.抽象类的基本定义 2.抽象类的使用原则 不会抽象类与接口,java = 没学 ...
- Swift-技巧(六)设置按钮状态并更改
摘要 按钮是一个宝藏控件,可以在设置的时候就对不同的状态添加图片.文本,甚至更改背景.在不同的展示场景中更改到不同的状态显示就好.恰恰是如何更改状态着实让我懵了一阵,所以记录一下过程.如果没有兴趣了解 ...
- 保姆级别的vue + ElementUI 搭建后台管理系统教程
vue + ElementUI 搭建后台管理系统记录 本文档记录了该系统从零配置的完整过程 项目源码请访问:https://gitee.com/szxio/vue2Admin,如果感觉对你有帮助,请点 ...
- [bzoj3170]松鼠聚会
这个距离就是切比雪夫距离,有一个神奇的东西是说将(x,y)变成(x+y,x-y),然后就是曼哈顿距离,因此转化后对x坐标和y坐标分别统计排序和求和(求前缀和预处理+二分) 1 #include< ...
- opencv 视频处理相关
包含视频格式知识(编解码和封装格式):如何获取视频信息及视频编解码格式:opencv读取及保存视频,及opencv fourcc编码格式 一.基础知识 视频的编解码格式和封装格式 参考如山似水 视频编 ...