annotation

package org.example.annotation;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyLog {

    /**
     * 日志内容
     */
    String logMsg() default "";

    /**
     * 类名(该方法没有默认值,不赋值会报检查异常)
     */
    Class<?> className();

    /**
     * 日志级别
     */
    LogType logType() default LogType.DEBUG;

    /**
     * 日志级别枚举
     */
    enum LogType {
        DEBUG,
        INFO,
        WARN,
        ERROR,
        FATAL
    }
}

package org.example.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
@Order(value = 1)
public class LogAspect {

@Pointcut(value = "@annotation(org.example.annotation.MyLog)")
public void annotationPointCut() {
}

@Around("annotationPointCut()")
public Object advice(ProceedingJoinPoint joinPoint) {
Object result = null;
System.out.println("环绕开始");
try {
// 获取所有参数
System.out.println("---------------参数列表开始-------------------------");
Object[] args = joinPoint.getArgs();

// 获取到方法的所有参数名称的字符串数组
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String[] parameterNames = signature.getParameterNames();

// 参数列表
for (int i = 0; i < parameterNames.length; i++) {
System.out.println("参数名:" + parameterNames[i] + " = " + args[i]);
}
System.out.println("---------------参数列表结束-------------------------");

// 自定义注解
System.out.println("---------------自定义注解开始-------------------------");
Method method = signature.getMethod();
MyLog myLog = method.getAnnotation(MyLog.class);
System.out.println("logMsg: " + myLog.logMsg());
System.out.println("className: " + myLog.className());
System.out.println("logType: " + myLog.logType());
System.out.println("---------------自定义注解结束-------------------------");

result = joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕结束");
return result;
}

@Before(value = "annotationPointCut()")
public void beforePointCut() {
System.out.println("beforePointCut()");
}

@After(value = "annotationPointCut()")
public void afterPointCut() {
System.out.println("afterPointCut()");
}

/**
* 结果返回之后
*
* @param myLog 注解对象
* @param result 业务返回值
*/
@AfterReturning(value = "annotationPointCut() && @annotation(myLog)", returning = "result")
public void afterPointCut(MyLog myLog, Object result) {
System.out.println("AfterReturning()");

System.out.println("logMsg: " + myLog.logMsg());
System.out.println("className: " + myLog.className());
System.out.println("logType: " + myLog.logType());
System.out.println(result);
}
}


package org.example.annotation;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "/")
public class TestController {

@MyLog(logMsg = "Enter testController#test()", className = TestController.class, logType = MyLog.LogType.INFO)
@RequestMapping(value = "testMyLog", method = RequestMethod.GET)
public String test(@RequestParam(value = "name", required = false, defaultValue = "yc") String name) {
System.out.println(name + "============");
return name;
}
}

测试:
http://localhost:8090/testMyLog?name=yyy
输出:

环绕开始
---------------参数列表开始-------------------------
参数名:name = yyy
---------------参数列表结束-------------------------
---------------自定义注解开始-------------------------
logMsg: Enter testController#test()
className: class org.example.annotation.TestController
logType: INFO
---------------自定义注解结束-------------------------
beforePointCut()
yyy============
环绕结束
afterPointCut()
AfterReturning()
logMsg: Enter testController#test()
className: class org.example.annotation.TestController
logType: INFO
yyy

 

 

说明:

@AfterReturning和@Around 一起使用时要注意  @Around 标注的方法是否有返回值,如果没有返回值则@AfterReturning中也获取不到返回值

posted @ 2022-02-10 17:57  刘维风  阅读(130)  评论(0)    收藏  举报