Spring Boot通过切面实现方法耗时情况

Spring Boot通过切面实现方法耗时情况

依赖

xml 复制代码
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>

自定义注解

java 复制代码
package com.geekmice.springbootselfexercise.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author PMB
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodExporter {
}

切面类

java 复制代码
package com.geekmice.springbootselfexercise.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: spring-boot-scaffold
 * @BelongsPackage: com.geekmice.com.geekmice.sbpagehelper.utils
 * @Author: pingmingbo
 * @CreateTime: 2023-07-26  21:52
 * @Description: TODO
 * @Version: 1.0
 */
@Aspect
@Component
@Slf4j
public class MethodExporterAspect {
    @Around("@annotation(com.geekmice.springbootselfexercise.annotation.MethodExporter)")
    public Object methodExporter(ProceedingJoinPoint point) throws Throwable {
        long start = System.currentTimeMillis() / 1000;
        Object proceed = point.proceed();
        long end = System.currentTimeMillis() / 1000;
        log.info("【耗时:】{}s", (end - start));
        return proceed;
    }
}

控制层

java 复制代码
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @PostMapping(value = "uploadFileByEasyPoi")
    @MethodExporter
    @ApiOperation(value = "通过easypoi上传文件")
    public AjaxResult uploadFileByEasyPoi(@RequestPart MultipartFile file){
        userService.uploadFileByEasyPoi(file);
        return AjaxResult.success();
    }
相关推荐
张涛酱107456几秒前
Spring AI 2.0.0-M3 新特性解析:MCP核心集成与重大升级
java
PFinal社区_南丞1 分钟前
一文讲透 .trae 文件夹 - Trae IDE 配置指南和最佳实践
后端
小刘不想改BUG12 分钟前
LeetCode 138.随机链表的复制 Java
java·leetcode·链表·hash table
NGC_661113 分钟前
Java 死锁预防:从原理到实战,彻底规避并发陷阱
java·开发语言
卓怡学长20 分钟前
m277基于java web的计算机office课程平台设计与实现
java·spring·tomcat·maven·hibernate
段小二21 分钟前
Spring AI Agent 完整实战:Function Calling + RAG + Memory + SafeGuard 构建机票助手
后端
编码忘我23 分钟前
Spring源码又看了一遍
后端
季明洵25 分钟前
Java简介与安装
java·开发语言
沉鱼.4428 分钟前
枚举问题集
java·数据结构·算法
林夕sama32 分钟前
多线程基础(五)
java·开发语言·前端