首页 springboot使用AOP基于注解记录操作日志(含源码)

springboot使用AOP基于注解记录操作日志(含源码)

举报
开通vip

springboot使用AOP基于注解记录操作日志(含源码)     springboot使用AOP基于注解记录操作日志(含源码)          spring框架让java开发者觉得非常简便,其中spring的两个特性IOC和AOP更是让我们的java代码变得十分得简洁,下面分享一个使用注解基于AOP记录操作日志目的自定义注解,只要添加了这个注解的请求在被请求时,都会有操作记录入库思路1.首先定义一个操作类型的枚举,操作包括增,删,改,查publicenumOperationType{SELECT("查询"),INSERT("插入"),UPDATE("更新"),DELE...

springboot使用AOP基于注解记录操作日志(含源码)
     springboot使用AOP基于注解 记录 混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载 操作日志(含源码)          spring框架让java开发者觉得非常简便,其中spring的两个特性IOC和AOP更是让我们的java代码变得十分得简洁,下面分享一个使用注解基于AOP记录操作日志目的自定义注解,只要添加了这个注解的请求在被请求时,都会有操作记录入库思路1.首先定义一个操作类型的枚举,操作包括增,删,改,查publicenumOperationType{SELECT("查询"),INSERT("插入"),UPDATE("更新"),DELETE("删除");privatefinalStringvalue;publicStringgetValue(){returnvalue;}OperationType(Stringvalue){this.value=value;}}2.自定义一个注解,有两个属性分别为title(标题)和type(操作类型)两个属性,作用域在方法上@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public@interfaceOperation{Stringtitle()default"";OperationTypevalue()defaultOperationType.SELECT;}3.定义一个操作类型实体,记录操作日志记录,字段包括标题,操作类型,方法名,ip,入参publicclassOperationRecordimplementsSerializable{privatestaticfinallongserialVersionUID=1L;/***标题*/privateStringtitle;/***操作类型*/privateStringtype;/***方法名*/privateStringmethod;/***真实ip*/privateStringip;/***入参类型和入参值*/privateMaprequestAndValue;publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetType(){returntype;}publicvoidsetType(Stringtype){this.type=type;}publicStringgetMethod(){returnmethod;}publicvoidsetMethod(Stringmethod){this.method=method;}publicStringgetIp(){returnip;}publicvoidsetIp(Stringip){this.ip=ip;}publicMapgetRequestAndValue(){returnrequestAndValue;}publicvoidsetRequestAndValue(MaprequestAndValue){this.requestAndValue=requestAndValue;}@OverridepublicStringtoString(){return"OperationRecord{"+"title='"+title+'\''+",type='"+type+'\''+",method='"+method+'\''+",ip='"+ip+'\''+",requestAndValue="+requestAndValue+'}';}}4.定义切面,入库操作@Slf4j@Aspect@ComponentpublicclassOperationAspect{privatestaticfinalStringUNKNOWN="unknown";@ResourceprivateOperationRecordServiceoperationRecordService;/***定义拦截点*/@Pointcut("@annotation(com.zyy.log.aop.annocation.Operation)")publicvoidpointcut(){}@Around("pointcut()")publicObjectrecordOperation(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{Objectobj=proceedingJoinPoint.proceed();try{//模拟入库OperationRecordoperationRecord=newOperationRecord();operationRecord.setTitle(getTitle(proceedingJoinPoint));operationRecord.setMethod(getMethodName(proceedingJoinPoint));operationRecord.setType(getOperationType(proceedingJoinPoint).getValue());operationRecord.setIp(getIp());operationRecord.setRequestAndValue(getNameAndValue(proceedingJoinPoint));operationRecordService.insert(operationRecord);System.out.println("operationRecord="+JSONUtil.toJsonStr(operationRecord));}catch(Exceptione){thrownewRuntimeException("入库失败");}returnobj;}/***获取注解**@paramproceedingJoinPoint*@return*/privateOperationgetAnnotation(ProceedingJoinPointproceedingJoinPoint){return((MethodSignature)proceedingJoinPoint.getSignature()).getMethod().getAnnotation(Operation.class);}/***获取操作类型**@paramproceedingJoinPoint*@return*/privateOperationTypegetOperationType(ProceedingJoinPointproceedingJoinPoint){returngetAnnotation(proceedingJoinPoint).value();}/***获取标题**@paramproceedingJoinPoint*@return*/privateStringgetTitle(ProceedingJoinPointproceedingJoinPoint){returngetAnnotation(proceedingJoinPoint).title();}/***获取方法名称**@paramproceedingJoinPoint*@return*/privateStringgetMethodName(ProceedingJoinPointproceedingJoinPoint){MethodSignaturemethodSignature=(MethodSignature)proceedingJoinPoint.getSignature();returnmethodSignature.getMethod().getDeclaringClass().getName()+"."+methodSignature.getMethod().getName();}/***获取真实ip*/privateStringgetIp(){ServletRequestAttributesattributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletRequestrequest=Objects.requireNonNull(attributes).getRequest();Stringip=request.getHeader("x-forwarded-for");if(ip==null||ip.length()==0||UNKNOWN.equalsIgnoreCase(ip)){ip=request.getHeader("Proxy-Client-IP");}if(ip==null||ip.length()==0||UNKNOWN.equalsIgnoreCase(ip)){ip=request.getHeader("WL-Proxy-Client-IP");}if(ip==null||ip.length()==0||UNKNOWN.equalsIgnoreCase(ip)){ip=request.getRemoteAddr();}Stringcomma=",";Stringlocalhost="127.0.0.1";if(ip.contains(comma)){ip=ip.split(",")[0];}if(localhost.equals(ip)){//获取本机真正的ip地址try{ip=InetAddress.getLocalHost().getHostAddress();}catch(UnknownHostExceptione){log.error(e.getMessage(),e);}}returnip;}/***获取方法参数名和参数值**@paramjoinPoint*@return*/privateMapgetNameAndValue(ProceedingJoinPointjoinPoint){finalSignaturesignature=joinPoint.getSignature();MethodSignaturemethodSignature=(MethodSignature)signature;finalString[]names=methodSignature.getParameterNames();finalObject[]args=joinPoint.getArgs();if(ArrayUtil.isEmpty(names)||ArrayUtil.isEmpty(args)){returnCollections.emptyMap();}if(names.length!=args.length){log.warn("{}方法参数名和参数值数量不一致",methodSignature.getName());returnCollections.emptyMap();}Mapmap=newHashMap<>();for(inti=0;i 总结 初级经济法重点总结下载党员个人总结TXt高中句型全总结.doc高中句型全总结.doc理论力学知识点总结pdf AOP非常强大,应用在很多场景。希望能帮助到大家 -全文完-
本文档为【springboot使用AOP基于注解记录操作日志(含源码)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
资教之佳
暂无简介~
格式:doc
大小:94KB
软件:Word
页数:24
分类:互联网
上传时间:2023-06-23
浏览量:4