100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > aop注解配置切点 spring_springboot aop 自定义注解方式实现一套完善的日志记录

aop注解配置切点 spring_springboot aop 自定义注解方式实现一套完善的日志记录

时间:2019-09-18 08:41:42

相关推荐

aop注解配置切点 spring_springboot aop 自定义注解方式实现一套完善的日志记录

一:功能简介

本文主要记录如何使用aop切面的方式来实现日志记录功能。

主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型(增删改查),详细描述,返回值。

二:项目结构图

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

三:代码实现

1.配置文件

这里只有两个配置:1)server.port=11000,设置项目启动的端口号,防止被其他服务占用;2)spring.aop.auto=true,开启spring的aop配置,简单明了,不需要多配置其他的配置或注解。application.yml文件server: port: 11000spring: aop: auto: true #启动aop配置

2.AOP切点类

这个是最主要的类,可以使用自定义注解或针对包名实现AOP增强。

1)这里实现了对自定义注解的环绕增强切点,对使用了自定义注解的方法进行AOP切面处理;

2)对方法运行时间进行监控;

3)对方法名,参数名,参数值,对日志描述的优化处理;

在方法上增加@Aspect注解声明切面,使用@Pointcut注解定义切点,标记方法。

使用切点增强的时机注解:@Before,@Around,@AfterReturning,@AfterThrowing,@After

package com.wwj.springboot.aop;import com.alibaba.fastjson.JSON;import com.wwj.springboot.annotation.OperationLogDetail;import com.wwj.springboot.model.OperationLog;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.aspectj.lang.reflect.MethodSignature;import org.ponent;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.UUID;/** * Created by IntelliJ IDEA * * @author weiwenjun * @date /9/12 */@Aspect@Componentpublic class LogAspect { /** * 此处的切点是注解的方式,也可以用包名的方式达到相同的效果 * '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")' */ @Pointcut("@annotation(com.wwj.springboot.annotation.OperationLogDetail)") public void operationLog(){} /** * 环绕增强,相当于MethodInterceptor */ @Around("operationLog()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { Object res = null; long time = System.currentTimeMillis(); try { res = joinPoint.proceed(); time = System.currentTimeMillis() - time; return res; } finally { try { //方法执行完成后增加日志 addOperationLog(joinPoint,res,time); }catch (Exception e){ System.out.println("LogAspect 操作失败:" + e.getMessage()); e.printStackTrace(); } } } private void addOperationLog(JoinPoint joinPoint, Object res, long time){ MethodSignature signature = (MethodSignature)joinPoint.getSignature(); OperationLog operationLog = new OperationLog(); operationLog.setRunTime(time); operationLog.setReturnValue(JSON.toJSONString(res)); operationLog.setId(UUID.randomUUID().toString()); operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs())); operationLog.setCreateTime(new Date()); operationLog.setMethod(signature.getDeclaringTypeName() + "." + signature.getName()); operationLog.setUserId("#{currentUserId}"); operationLog.setUserName("#{currentUserName}"); OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class); if(annotation != null){ operationLog.setLevel(annotation.level()); operationLog.setDescribe(getDetail(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation)); operationLog.setOperationType(annotation.operationType().getValue()); operationLog.setOperationUnit(annotation.operationUnit().getValue()); } //TODO 这里保存日志 System.out.println("记录日志:" + operationLog.toString());// operationLogService.insert(operationLog); } /** * 对当前登录用户和占位符处理 * @param argNames 方法参数名称数组 * @param args 方法参数数组 * @param annotation 注解信息 * @return 返回处理后的描述 */ private String getDetail(String[] argNames, Object[] args, OperationLogDetail annotation){ Map map = new HashMap<>(4); for(int i = 0;i < argNames.length;i++){ map.put(argNames[i],args[i]); } String detail = annotation.detail(); try { detail = "'" + "#{currentUserName}" + "'=》" + annotation.detail(); for (Map.Entry entry : map.entrySet()) { Object k = entry.getKey(); Object v = entry.getValue(); detail = detail.replace("{{" + k + "}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。