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();
    }
相关推荐
一起养小猫6 分钟前
《Java数据结构与算法》第三篇(下)队列全解析:从基础概念到高级应用
java·开发语言·数据结构
用户638982245897 分钟前
使用Hutool的ExcelWriter导出复杂模板,支持下拉选项级联筛选
后端
程序员鱼皮9 分钟前
10个免费的网站分析工具,竟然比付费的更香?
后端·程序员·数据分析
vx_vxbs6614 分钟前
【SSM电动车智能充电服务平台】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·mysql·spring cloud·小程序·php·idea
码一行17 分钟前
Eino AI 实战: Eino 的文档加载与解析
后端·go
码一行17 分钟前
Eino AI 实战:DuckDuckGo 搜索工具 V1 与 V2
后端·go
未秃头的程序猿18 分钟前
🚀 设计模式在复杂支付系统中的应用:策略+工厂+模板方法模式实战
后端·设计模式
叹隙中驹石中火梦中身18 分钟前
解耦神器Event和EventListener
java
踏浪无痕18 分钟前
@Transactional的5种失效场景和自检清单
spring boot·后端·spring cloud
Boop_wu27 分钟前
[Java EE] 多线程进阶(JUC)(2)
java·jvm·算法