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();
    }
相关推荐
学IT的周星星1 分钟前
Spring 框架整合 JUnit 单元测试
java·spring·junit·单元测试
bcbnb6 分钟前
Fiddler抓包工具使用教程,HTTPHTTPS抓包、代理配置与调试技巧全解析(附实战经验)
后端
w_t_y_y9 分钟前
主流模型调用
java
昔我往昔12 分钟前
Gateway整合knife4报错404 (Not Found)
java
三口吃掉你13 分钟前
微服务之网关(Spring Cloud Gateway)
java·网关·微服务·gateway
Kent_J_Truman24 分钟前
JDK Maven Tomcat Spring在VSCode中的部分配置细节(自用)
java·tomcat·maven
邂逅星河浪漫28 分钟前
【Java】异常详解+实例演示+知识总结
java·异常·exception
虎子_layor32 分钟前
PostgreSQL这么多优势,为什么还要使用MySQL
后端·sql
她说人狗殊途33 分钟前
面试题001
java
Han.miracle35 分钟前
Java的多线程——多线程(3)线程安全
java·开发语言·jvm·学习·安全·线程·多线程