springboot aop 自定義注解方式實(shí)現(xiàn)一套完善的日志記錄

一:功能簡介

本文主要記錄如何使用aop切面的方式來實(shí)現(xiàn)日志記錄功能。

主要記錄的信息有: 操作人,方法名,參數(shù),運(yùn)行時間,操作類型(增刪改查),詳細(xì)描述,返回值。

二:項(xiàng)目結(jié)構(gòu)圖

如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。

三:代碼實(shí)現(xiàn)

1.配置文件

這里只有兩個配置:1)server.port=11000,設(shè)置項(xiàng)目啟動的端口號,防止被其他服務(wù)占用;2)spring.aop.auto=true,開啟spring的aop配置,簡單明了,不需要多配置其他的配置或注解。application.yml文件server: port:11000spring: aop: auto:true#啟動aop配置

2.AOP切點(diǎn)類

這個是最主要的類,可以使用自定義注解或針對包名實(shí)現(xiàn)AOP增強(qiáng)。

1)這里實(shí)現(xiàn)了對自定義注解的環(huán)繞增強(qiáng)切點(diǎn),對使用了自定義注解的方法進(jìn)行AOP切面處理;

2)對方法運(yùn)行時間進(jìn)行監(jiān)控;

3)對方法名,參數(shù)名,參數(shù)值,對日志描述的優(yōu)化處理;

在方法上增加@Aspect?注解聲明切面,使用@Pointcut?注解定義切點(diǎn),標(biāo)記方法。

使用切點(diǎn)增強(qiáng)的時機(jī)注解:@Before,@Around,@AfterReturning,@AfterThrowing,@After

packagecom.wwj.springboot.aop;importcom.alibaba.fastjson.JSON;importcom.wwj.springboot.annotation.OperationLogDetail;importcom.wwj.springboot.model.OperationLog;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.stereotype.Component;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjava.util.UUID;/**

* Created by IntelliJ IDEA

*

* @author weiwenjun

* @date 2018/9/12

*/@Aspect@Componentpublicclass LogAspect {/**

* 此處的切點(diǎn)是注解的方式,也可以用包名的方式達(dá)到相同的效果

* '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")'

*/@Pointcut("@annotation(com.wwj.springboot.annotation.OperationLogDetail)")publicvoidoperationLog(){}/**

* 環(huán)繞增強(qiáng),相當(dāng)于MethodInterceptor

*/@Around("operationLog()")publicObjectdoAround(ProceedingJoinPoint joinPoint)throwsThrowable {Objectres =null;longtime = System.currentTimeMillis();try{ res = joinPoint.proceed(); time = System.currentTimeMillis() - time;returnres; }finally{try{//方法執(zhí)行完成后增加日志addOperationLog(joinPoint,res,time); }catch(Exception e){ System.out.println("LogAspect 操作失?。?+ e.getMessage()); e.printStackTrace(); } } }privatevoidaddOperationLog(JoinPoint joinPoint,Objectres,longtime){ MethodSignature signature = (MethodSignature)joinPoint.getSignature(); OperationLog operationLog =newOperationLog(); operationLog.setRunTime(time); operationLog.setReturnValue(JSON.toJSONString(res)); operationLog.setId(UUID.randomUUID().toString()); operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs())); operationLog.setCreateTime(newDate()); 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);}/**

* 對當(dāng)前登錄用戶和占位符處理

* @param argNames 方法參數(shù)名稱數(shù)組

* @param args 方法參數(shù)數(shù)組

* @param annotation 注解信息

* @return 返回處理后的描述

*/privateStringgetDetail(String[] argNames,Object[] args, OperationLogDetail annotation){ Mapmap=newHashMap<>(4);for(inti =0;i < argNames.length;i++){map.put(argNames[i],args[i]); }Stringdetail = annotation.detail();try{ detail ="'"+"#{currentUserName}"+"'=》"+ annotation.detail();for(Map.Entry entry :map.entrySet()) {Objectk = entry.getKey();Objectv = entry.getValue(); detail = detail.replace("{{"+ k +"}}", JSON.toJSONString(v)); } }catch(Exception e){ e.printStackTrace(); }returndetail; } @Before("operationLog()")publicvoiddoBeforeAdvice(JoinPoint joinPoint){ System.out.println("進(jìn)入方法前執(zhí)行....."); }/**

* 處理完請求,返回內(nèi)容

* @param ret

*/@AfterReturning(returning ="ret", pointcut ="operationLog()")publicvoiddoAfterReturning(Objectret) { System.out.println("方法的返回值 : "+ ret); }/**

* 后置異常通知

*/@AfterThrowing("operationLog()")publicvoidthrowss(JoinPoint jp){ System.out.println("方法異常時執(zhí)行....."); }/**

* 后置最終通知,final增強(qiáng),不管是拋出異?;蛘哒M顺龆紩?zhí)行

*/@After("operationLog()")publicvoidafter(JoinPoint jp){ System.out.println("方法最后執(zhí)行....."); }}

3.自定義注解

package com.wwj.springboot.annotation;import com.wwj.springboot.enums.OperationType;import com.wwj.springboot.enums.OperationUnit;import java.lang.annotation.*;/**

* Created by IntelliJ IDEA

*

* @author weiwenjun

* @date 2018/9/12*/

//@OperationLogDetail(detail = "通過手機(jī)號[{{tel}}]獲取用戶名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)

@Documented

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface OperationLogDetail {

/** * 方法描述,可使用占位符獲取參數(shù):{{tel}}*/

String detail() default "";

/** * 日志等級:自己定,此處分為1-9*/

int level() default 0;

/** * 操作類型(enum):主要是select,insert,update,delete*/

OperationType operationType() default OperationType.UNKNOWN;

/** * 被操作的對象(此處使用enum):可以是任何對象,如表名(user),或者是工具(redis)*/

OperationUnit operationUnit() default OperationUnit.UNKNOWN;

}

4.注解用到的枚舉類型

package com.wwj.springboot.enums;/**

* Created by IntelliJ IDEA

*

* @author weiwenjun

* @date 2018/9/12

*/publicenumOperationType {/**

* 操作類型

*/UNKNOWN("unknown"), DELETE("delete"), SELECT("select"), UPDATE("update"), INSERT("insert");privateStringvalue;publicStringgetValue(){returnvalue; }publicvoidsetValue(Stringvalue){this.value=value; } OperationType(String s) {this.value= s; }}package com.wwj.springboot.enums;/**

* Created by IntelliJ IDEA

* 被操作的單元

* @author weiwenjun

* @date 2018/9/12

*/publicenumOperationUnit {/**

* 被操作的單元

*/UNKNOWN("unknown"), USER("user"), EMPLOYEE("employee"), Redis("redis");privateStringvalue; OperationUnit(Stringvalue) {this.value=value; }publicStringgetValue(){returnvalue; }publicvoidsetValue(Stringvalue){this.value=value; }}

5.日志記錄對象

如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。

package com.wwj.springboot.model;importjava.util.Date;/**

* Created by IntelliJ IDEA

*

* @author weiwenjun

* @date 2018/9/12

*/publicclassOperationLog {privateStringid;privateDatecreateTime;/**

* 日志等級

*/privateInteger level;/**

* 被操作的對象

*/privateStringoperationUnit;/**

* 方法名

*/privateStringmethod;/**

* 參數(shù)

*/privateStringargs;/**

* 操作人id

*/privateStringuserId;/**

* 操作人

*/privateStringuserName;/**

* 日志描述

*/privateStringdescribe;/**

* 操作類型

*/privateStringoperationType;/**

* 方法運(yùn)行時間

*/privateLong runTime;/**

* 方法返回值

*/privateStringreturnValue;@OverridepublicStringtoString() {return"OperationLog{"+"id='"+ id +'\''+", createTime="+ createTime +", level="+ level +", operationUnit='"+ operationUnit +'\''+", method='"+ method +'\''+", args='"+ args +'\''+", userId='"+ userId +'\''+", userName='"+ userName +'\''+", describe='"+ describe +'\''+", operationType='"+ operationType +'\''+", runTime="+ runTime +", returnValue='"+ returnValue +'\''+'}'; }publicLong getRunTime() {returnrunTime; }publicvoidsetRunTime(Long runTime) {this.runTime = runTime; }publicStringgetReturnValue() {returnreturnValue; }publicvoidsetReturnValue(StringreturnValue) {this.returnValue = returnValue; }publicStringgetId() {returnid; }publicvoidsetId(Stringid) {this.id = id; }publicDategetCreateTime() {returncreateTime; }publicvoidsetCreateTime(DatecreateTime) {this.createTime = createTime; }publicInteger getLevel() {returnlevel; }publicvoidsetLevel(Integer level) {this.level = level; }publicStringgetOperationUnit() {returnoperationUnit; }publicvoidsetOperationUnit(StringoperationUnit) {this.operationUnit = operationUnit; }publicStringgetMethod() {returnmethod; }publicvoidsetMethod(Stringmethod) {this.method = method; }publicStringgetArgs() {returnargs; }publicvoidsetArgs(Stringargs) {this.args = args; }publicStringgetUserId() {returnuserId; }publicvoidsetUserId(StringuserId) {this.userId = userId; }publicStringgetUserName() {returnuserName; }publicvoidsetUserName(StringuserName) {this.userName = userName; }publicStringgetDescribe() {returndescribe; }publicvoidsetDescribe(Stringdescribe) {this.describe = describe; }publicStringgetOperationType() {returnoperationType; }publicvoidsetOperationType(StringoperationType) {this.operationType = operationType; }}

6.springboot啟動類

packagecom.wwj.springboot;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;/** * Created by IntelliJ IDEA * *@authorweiwenjun *@date2018/9/12 */@SpringBootApplicationpublicclassAopApplication{publicstaticvoidmain(String[] args) { SpringApplication.run(AopApplication.class); }}

7.controller類

packagecom.wwj.springboot.controller;importcom.wwj.springboot.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;/**

* Created by IntelliJ IDEA

*

* @author weiwenjun

* @date 2018/9/12

*/@Controller@RequestMapping("user")public class UserController { @Autowiredprivate UserService userService;/**

* 訪問路徑 http://localhost:11000/user/findUserNameByTel?tel=1234567

* @param tel 手機(jī)號

* @return userName

*/@ResponseBody@RequestMapping("/findUserNameByTel") public String findUserNameByTel(@RequestParam("tel") String tel){returnuserService.findUserName(tel); }}

8.Service類

packagecom.wwj.springboot.service;/** * Created by IntelliJ IDEA * *@authorweiwenjun *@date2018/9/13 */publicinterfaceUserService{/** * 獲取用戶信息 *@return*@paramtel */StringfindUserName(String tel);}

9.ServiceImpl類

packagecom.wwj.springboot.service.impl;importcom.wwj.springboot.annotation.OperationLogDetail;importcom.wwj.springboot.enums.OperationType;importcom.wwj.springboot.enums.OperationUnit;importcom.wwj.springboot.service.UserService;importorg.springframework.stereotype.Service;/**

* Created by IntelliJ IDEA

*

* @author weiwenjun

* @date 2018/9/13

*/@Servicepublic class UserServiceImpl implements UserService { @OperationLogDetail(detail="通過手機(jī)號[{{tel}}]獲取用戶名",level =3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT) @Override public String findUserName(String tel) {System.out.println("tel:"+tel);return"zhangsan"; }}

四:MAVEM依賴

本項(xiàng)目有兩個pom文件,父類的pom文件主要作用是對子類pom文件依賴的版本號進(jìn)行統(tǒng)一管理。

1.最外層的pom文件配置如下

<?xml version="1.0"encoding="UTF-8"?>4.0.0com.wwj.springbootspringbootpom1.0-SNAPSHOTspringboot-aop<!-- Inherit defaults from Spring Boot -->org.springframework.bootspring-boot-starter-parent2.0.4.RELEASE1.2.49<!-- Import dependency management from Spring Boot -->org.springframework.bootspring-boot-dependencies2.0.4.RELEASEpomimportcom.alibabafastjson${fastjson.version}

2.子pom文件配置如下

<?xml version="1.0"encoding="UTF-8"?>springbootcom.wwj.springboot1.0-SNAPSHOT4.0.0springboot-aoporg.springframework.bootspring-boot-starter-web<!-- spring-boot aop依賴配置引入 -->org.springframework.bootspring-boot-starter-aopcom.alibabafastjson

五:運(yùn)行結(jié)果

進(jìn)入方法前執(zhí)行.....tel:1234567記錄日志:OperationLog{id='cd4b5ba7-7580-4989-a75e-51703f0dfbfc', createTime=Fri Sep1408:54:55CST2018, level=3, operationUnit='user', method='com.wwj.springboot.service.impl.UserServiceImpl.findUserName', args='["1234567"]', userId='#{currentUserId}', userName='#{currentUserName}', describe=''#{currentUserName}'=》通過手機(jī)號["1234567"]獲取用戶名', operationType='select', runTime=4, returnValue='"zhangsan"'}方法最后執(zhí)行.....方法的返回值 : zhangsan

如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容