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();
    }
相关推荐
lizhongxuan1 小时前
Claude Code 防上下文爆炸:源码级深度解析
前端·后端
Via_Neo1 小时前
JAVA中以2为底的对数表示方式
java·开发语言
Warson_L2 小时前
Python 流程控制与逻辑
后端·python
糖炒栗子03262 小时前
架构笔记:应用配置无状态化 (Statelessness)
后端
野生技术架构师2 小时前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
Warson_L2 小时前
Python 四大组合数据类型 (Collection Types)
后端·python
廋到被风吹走2 小时前
【AI】Codex 多语言实测:Python/Java/JS/SQL 效果横评
java·人工智能·python
tERS ERTS3 小时前
MySQL中查看表结构
java
坊钰3 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
查古穆3 小时前
大白话讲ReAct:大模型的“边想边干”
后端