当前位置: 首页 > news >正文

Java之~ Aop自定义注解日志

大纲步骤:
一,创建需要记录的日志表,创建基础方法。(省略)
二,在需要加记录日志的方法上加Aop注解1,创建一个注解类,Aop中定义一个注解
import java.lang.annotation.*;
/*** http 请求第三方请求日志使用注解*/
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HttpLog {//需要手动指定的枚举类HttpLogTypeEnum type();
}

2,切面处理

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
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.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.SpringContextUtils;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
/*** http第三方日志,切面处理类** @Author scott* @email jeecgos@163.com* @Date 2018年1月14日*/
@Aspect
@Component
@Slf4j
public class HttpLogAspect {@Autowiredprivate SysThirdHttpLogApi httpLogApi;@Pointcut("@annotation(org.jeecg.common.aspect.annotation.HttpLog)")public void httpLogPointCut() {}@Around("httpLogPointCut()")public Object around(ProceedingJoinPoint point) throws Throwable {long beginTime = System.currentTimeMillis();//执行方法 目标方法返回值Object result = point.proceed();//执行时长(毫秒)long time = System.currentTimeMillis() - beginTime;//保存http响应日志saveSysHttpLog(point, time,result);//        log.info(String.valueOf(result));return result;}//处理数据,入库保存日志private void saveSysHttpLog(ProceedingJoinPoint joinPoint, long time,Object result) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();//获取注解类HttpLog httpLog = method.getAnnotation(HttpLog.class);if (httpLog != null) {HttpLogDto dto = new HttpLogDto();dto.setType(httpLog.type().name());//方法名//dto.setMethodType(httpLog.methodType().name());//dto.setMethodName(httpLog.methodType().getDesc());//请求的方法名String className = joinPoint.getTarget().getClass().getName();String methodName = signature.getName();dto.setMethod(className + "." + methodName + "()");//获取requestObject[] request = joinPoint.getArgs();//请求的参数dto.setRequestBody(JSONObject.toJSONString(request));//响应的结果String res = JSONObject.toJSONString(result);dto.setResponseResult(res);dto.setCostTime(time);.......//添加日志httpLogApi.insert(dto);}}

演示:

 

 

 

http://www.lryc.cn/news/60772.html

相关文章:

  • 编译原理个人作业--第四章
  • 学习笔记:数据库简介
  • day18_集合
  • Go面试必会基础题
  • 发送封包协议实现XXZ批量秒分解装备
  • Spring学习——Nginx
  • 记录 vue-cli 安装过程
  • 含氢微网优化调度模型matlab
  • 【springcloud开发教程】路由网关——zuul
  • DF竞赛平台携手嬴彻科技与清华大学智能产业研究院,助力自动驾驶挑战赛圆满落幕!
  • 234:vue+openlayers 加载本地shp数据,在map上显示图形
  • 网络模型-网络体系结构(OSI、TCP/IP)
  • 园区智慧导览地图软件,智慧工厂导航定位怎么解决方案的
  • Redis高可用之3种集群方案对比
  • java 线程唤醒于阻塞的常用方法
  • 面包多面包多面包多面包多面包多面包多
  • windows下Tomcat安装
  • 4月17号软件资讯更新合集.....
  • [java基础]面向对象(五)
  • React应用(基于React脚手架)
  • Redis(03)List--附有示例
  • openEuler-linux下部署zabbix-超级详细
  • nginx 简介 第四章
  • c++ float32 与 float16 互转
  • Redis问题
  • [API]ListList方法集合排序Lambda表达式(四)
  • 【ChatGPT】无需魔法打开即用的 AI 工具集锦
  • Choco-slover的使用
  • 亚马逊、ebay、temu如何提升产品点击率?测评自养号解析
  • 人工智能的前沿信息获取之使用谷歌学术搜索