项目结构:
项目相关类:
注解类:
package com.zz.technology.aopLog.annotation;
import com.zz.technology.aopLog.enums.OperationType;
import com.zz.technology.aopLog.enums.OperationUnit;
import java.lang.annotation.*;
/**
*
* //@OperationLogDetail(detail = "通过手机号[{
{tel}}]获取用户名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
*/
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLogDetail {
/**
* 方法描述,可使用占位符获取参数:{
{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;
}
aop切入点类:
package com.zz.technology.aopLog.aop;
import com.alibaba.fastjson.JSON;
import com.zz.technology.aopLog.annotation.OperationLogDetail;
import com.zz.technology.aopLog.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.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* 1)这里实现了对自定义注解的环绕增强切点,对使用了自定义注解的方法进行AOP切面处理;
* 2)对方法运行时间进行监控;
* 3)对方法名,参数名,参数值,对日志描述的优化处理;
*
* 在方法上增加@Aspect 注解声明切面,使用@Pointcut 注解定义切点,标记方法。
* 使用切点增强的时机注解:@Before,@Around,@AfterReturning,@AfterThrowing,@After
*/
@Aspect
@Component
public class LogAspect {
/**
* 此处的切点是注解的方式,也可以用包名的方式达到相同的效果
* '@Pointcut("execution(* com.zz.technology.aopLog.service.impl.*.*(..))")'
*/
@Pointcut("@annotation(com.zz.technology.aopLog.annotation.OperationLogDetail)")
public void operationLog(){}
/**
* 环绕增强,相当于MethodInterceptor
*/
@Around("operationLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object res = null;