自定义注解AOP
package com.log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author Tobieance
* @description 日志注解切面类
* @date 2023-09-07 16:46
*/
@Component
@Aspect
public class LogAspect {
@Pointcut("@annotation(com.log.MyLog)")//注解
public void pointcut() {
}
@Before("pointcut()")
public void log(JoinPoint joinPoint) throws NoSuchMethodException {
//获取目标对象
Object target = joinPoint.getTarget();
//获取目标对象方法
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = target.getClass().getMethod(
methodSignature.getName(),
methodSignature.getParameterTypes());
//获取注解对象实例
MyLog log = method.getAnnotation(MyLog.class);
String type = log.type();
String desc = log.desc();
System.out.println("日志类型:" + type + "\t\t调用方法" + method.getName() + "\t日志描述" + desc);
}
}
package com.log;
import java.lang.annotation.*;
/**
* @author Tobieance
* @description MyLog注解
* @date 2023-09-07 16:36
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyLog {
String type();//日志类型
String desc();//日志描述
}
标签:lang,log,自定义,org,AOP,import,注解,aspectj
From: https://www.cnblogs.com/tobieance/p/17744448.html