springboot利用AOP实现mymes的接口日志

根据之前讲使用springboot整合AOP记录访问mymes日志,没看过的可以回顾前面的文章

SpringBoot使用AOP记录接口访问mymes日志

本章主要概述mymes项目中使用AOP记录接口日志,通过在Controller层建立一个切面来实现接口的统一访问日志记录

AOP(面向切面编程)

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AOP相关术语

1.Advice(通知)

通知描述了切面要完成的工作以及何时执行。比如我们的日志切面需要记录每个接口调用时长,就需要在接口调用前后分别记录当前时间,再取差值。

  • 前置通知(Before):在目标方法调用前调用通知功能;
  • 后置通知(After):在目标方法调用之后调用通知功能,不关心方法的返回结果;
  • 返回通知(AfterReturning):在目标方法成功执行之后调用通知功能;
  • 异常通知(AfterThrowing):在目标方法抛出异常后调用通知功能;
  • 环绕通知(Around):通知包裹了目标方法,在目标方法调用之前和之后执行自定义的行为。

2.JoinPoint(连接点)

所谓的连接点,就是指被拦截到的点,在springboot中,指的就是方法,因为springboot只支持方法类型的连接点,例如:裂口方法被调用的时候就是日志切面连接点。

3.Pointcut(切点)

所谓切入点是指需要进行增强的连接点(Joinpoint),定义了通知功能被应用的范围。例如:日志切面的应用范围就是所有接口,即所有controller层的接口方法。

4.Aspect(切面)

切面就是Advice(通知)+Pointcut(切点),定义了什么时候去通知功能

5.Introduction(引入)

允许我们向现有的类添加新方法属性。这不就是把切面(也就是新方法属性:通知定义的)用到目标类中吗

6.Weaving(织入)

把切面应用到目标对象来创建新的代理对象的过程。

使用注解方式创建注解切面

切面注解

  • @Aspect:定义切面
  • @Before: 在目标方法调用前,该通知方法会执行
  • @After: 在目标方法调用后,该通知方法会执行
  • @AfterReturning: 在目标方法返回后,该通知方法会执行
  • @BeforetThrowing: 在目标方法调用并抛出异常后,该通知方法会执行
  • @Around: 该方法会将目标注解封装起来
  • @Pointcut:定义切点

切点:

指定通知被使用的范围,注解格式:

execution(方法修饰符 返回类型 方法所属的包.类名.方法名称(方法参数)
//com.cn.mymes.controller包中所有类的public方法都应用切面里的通知
execution(public * com.cn.mymes.controller.*.*(..))
//com.cn.mymes.service包及其子包下所有类中的所有方法都应用切面里的通知
execution(* com.cn.mymes.service..*.*(..))
//com.cn.mymes.service.MyMesBrandService类中的所有方法都应用切面里的通知
execution(* com.cn.mymes.service.MyMesBrandService.*(..))

在mymes中添加AOP切面实现mymes接口切面日志记录

添加日志信息封装类MyMesLog

用于封装序列记录的日志信息,包括操作描述,时间,url,参数,返回结果等信息

package com.cn.mymes.dto;
/**
* Controller层的日志封装类
* Created by zbb on 2021/1/3
*/
public class MyMesLog {
    /**
    * 操作描述
    */
    private String description;

    /**
    * 操作用户
    */
    private String username;

    /**
    * 操作时间
    */
    private  Long  startTime;

    /**
    * 消耗时间
    */
    private Integer spendTime;
    /**
    * 根路径
    */
    private String  basePath;

    /**
    * URI
    */
    private String uri;
    /**
    * URL
    */
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
    * 请求类型
    */
    private String method;

    /**
    * IP地址
    */
    private String ip;

    /**
    * 请求参数
    */
    private Object parameter;

    /**
    * 请求返回的结果
    */
    private Object result;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public Integer getSpendTime() {
        return spendTime;
    }

    public void setSpendTime(Integer spendTime) {
        this.spendTime = spendTime;
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Object getParameter() {
        return parameter;
    }

    public void setParameter(Object parameter) {
        this.parameter = parameter;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
}

添加切面MyMesLogAspect

定义日志切面,在环绕通知中获取日志需要的信息,并应用到controller层中所有的public方法中去。

package com.cn.mymes.component;

/*
* ------AOP---------------
* AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程
* 序功能的统一维护的一
* 种技术。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,
* 提高程序的可重用性,同时提高了开发的效率。
*1.前置通知(Before):在目标方法调用前调用通知功能;
*2.后置通知(After):在目标方法调用之后调用通知功能,不关心方法的返回结果;
*3.返回通知(AfterReturning):在目标方法成功执行之后调用通知功能;、
*4.异常通知(AfterThrowing):在目标方法抛出异常后调用通知功能;
*5.环绕通知(Around):通知包裹了目标方法,在目标方法调用之前和之后执行自定义的行为。
*6.连接点(JoinPoint) 通知功能被应用的时机。比如接口方法被调用的时候就是日志切面的连接点。
*7.切点(Pointcut) 切点定义了通知功能被应用的范围。比如日志切面的应用范围就是所有接口,即所有controller层的接口方法
*8.切面(Aspect)切面是通知和切点的结合,定义了何时、何地应用通知功能。
*9.引入(Introduction) 在无需修改现有类的情况下,向现有的类添加新方法或属性。
*10.织入(Weaving)  把切面应用到目标对象并创建新的代理对象的过程。
*-----------切点表达式
* execution(方法修饰符 返回类型 方法所属的包.类名.方法名称(方法参数)
*/
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONUtil;
import com.cn.mymes.dto.MyMesLog;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 统一日志处理切面
* Created by zbb on 2021/1/3
*/
@Aspect
@Component
@Order(1)
public class MyMesLogAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyMesLogAspect.class);

    @Pointcut("execution(public * com.cn.mymes.controller.*.*(..))")
    public void MyMesLog() {
    }

    @Before("MyMesLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    }

    @AfterReturning(value = "MyMesLog()", returning = "ret")
    public void doAfterReturning(Object ret) throws Throwable {
    }

    @Around("MyMesLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        //获取当前请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //记录请求信息
        MyMesLog webLog = new MyMesLog();
        Object result = joinPoint.proceed();
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method.isAnnotationPresent(ApiOperation.class)) {
            ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
            webLog.setDescription(apiOperation.value());
        }
        long endTime = System.currentTimeMillis();
        String urlStr = request.getRequestURL().toString();
        webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
        webLog.setIp(request.getRemoteUser());
        webLog.setMethod(request.getMethod());
        webLog.setParameter(getParameter(method, joinPoint.getArgs()));
        webLog.setResult(result);
        webLog.setSpendTime((int) (endTime - startTime));
        webLog.setStartTime(startTime);
        webLog.setUri(request.getRequestURI());
        webLog.setUrl(request.getRequestURL().toString());
        LOGGER.info("{}", JSONUtil.parse(webLog));
        return result;
    }

    /**
    * 根据方法和传入的参数获取请求参数
    */
    private Object getParameter(Method method, Object[] args) {
        List<Object> argList = new ArrayList<>();
        Parameter[] parameters = method.getParameters();
        for (int i = 0; i < parameters.length; i++) {
            //将RequestBody注解修饰的参数作为请求参数
            RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
            if (requestBody != null) {
                argList.add(args[i]);
            }
            //将RequestParam注解修饰的参数作为请求参数
            RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
            if (requestParam != null) {
                Map<String, Object> map = new HashMap<>();
                String key = parameters[i].getName();
                if (!StringUtils.isEmpty(requestParam.value())) {
                    key = requestParam.value();
                }
                map.put(key, args[i]);
                argList.add(map);
            }
        }
        if (argList.size() == 0) {
            return null;
        } else if (argList.size() == 1) {
            return argList.get(0);
        } else {
            return argList;
        }
    }
}

运行项目

访问Swagger 接口地址http://localhost:9999/swagger-ui.html ,测试接口。

控制台上显示的调用信息

{
    "basePath": "http://localhost:9999",
    "description": "获取验证码",
    "method": "GET",
    "parameter": {
        "telephone": "XXXXXXXXXX"
    },
    "result": {
        "code": 200,
        "data": "010146",
        "message": "获取验证码成功"
    },
    "spendTime": 849,
    "startTime": 1609686183897,
    "uri": "/sso/getAuthCode",
    "url": "http://localhost:9999/sso/getAuthCode"
}


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,701评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,649评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,037评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,994评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,018评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,796评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,481评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,370评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,868评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,014评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,153评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,832评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,494评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,039评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,437评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,131评论 2 356

推荐阅读更多精彩内容