自定义注解+AOP记录访问日志

1、问题描述

AOP拦截请求,记录请求参数、返回参数及响应时间,以前已经介绍过了,但是发现有个小问题,就是有些心跳或者仅仅是查询字典下拉框的请求,本身不用记录,排除的话,需要一个一个排除,有点麻烦,就用自定义注解对aop记录请求与返回做了个下改进,简单记录下。

2、解决方案

简单说就是新建个自定义注解标签,然后对需要记录日志的的方法加上标签就可以了。

2.1 自定义标签

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface WebLogAnotation {
}

简单说明: @Target({ElementType.METHOD}),这个是定义使用范围;

@Target:注解的作用目标        
@Target(ElementType.TYPE)   //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR)  //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包   

2.2 修改AOP中的拦截方法

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Aspect
@Component
public class WebLogAspect {

    //@Pointcut("execution(public * com.spring.wx.oauth.conntroller.*.*(..))")
    @Pointcut("@annotation(com.spring.wx.oauth.utils.WebLogAnotation)")
    public void webLog(){

    }
    //@Around:环绕通知
    @Around("webLog()")
    public Object saveSysLog(ProceedingJoinPoint proceedingJoinPoint) {

        System.out.println("环绕通知开始。。。。。");

        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();

        String className = proceedingJoinPoint.getTarget().getClass().getName();
        String methodName = method.getName();

        System.out.println(className);
        System.out.println(methodName);
        System.out.println(className + "." + methodName);

        //请求的参数
        Object[] args = proceedingJoinPoint.getArgs();
        String params = JSON.toJSONString(args);
        System.out.println(params);

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 记录下请求内容
        System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());

        //记录时间
        long start = System.currentTimeMillis();
        Object result =null;
        try {
            result = proceedingJoinPoint.proceed();
            System.out.println(result.toString());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println(throwable.getMessage());
        }

        Long time = System.currentTimeMillis() - start;
        System.out.println(time);
        System.out.println("环绕通知结束。。。。。");
        return result;
    }

}

AOP拦截的前面已经介绍过好几次,就不多说了,修改以下这行,其中pointcut里面是楼上定义的注解方法;

  @Pointcut("@annotation(com.spring.wx.oauth.utils.WebLogAnotation)")
    public void webLog(){

    }

2.3 Controller使用自定义注解

import com.spring.wx.oauth.utils.WebLogAnotation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest")
public class RestConntroller {
    @WebLogAnotation
    @RequestMapping("/weblog")
    public String  weblog() {
        return "软件老王欢迎你!";
    }
}

其中关键是 @WebLogAnotation这个注解,只有添加了这个注解的请求才会走到AOP记录日志方法;

2.4 验证

当通过页面请求weblog方法的时候,就会进入AOP拦截方法记录日志;


更多信息请关注公众号:「软件老王」,关注不迷路,软件老王和他的IT朋友们,分享一些他们的技术见解和生活故事。

更多信息请关注公众号:「软件老王」,关注不迷路,IT技术与相关干货分享,回复关键字获取对应干货,本文版权归作者软件老王所有,转载需注明作者、超链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/ruanjianlaowang/p/15635472.html