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();
    }
相关推荐
清风徐来QCQ2 分钟前
Java笔试总结一
java·开发语言
10Eugene26 分钟前
C++/Qt自制八股文
java·开发语言·c++
程序员小假32 分钟前
为什么要有 time _wait 状态,服务端这个状态过多是什么原因?
java·后端
qwert10371 小时前
跨域问题解释及前后端解决方案(SpringBoot)
spring boot·后端·okhttp
yuweiade2 小时前
【Spring】Spring MVC案例
java·spring·mvc
90后的晨仔2 小时前
OpenClaw Windows 完整安装指南
后端
码喽7号2 小时前
springboot学习四:RESTful风格+swagger
spring boot·学习·restful
罗超驿2 小时前
Java数据结构_链表
java·数据结构·链表
小璐资源网3 小时前
C++中如何正确区分`=`和`==`的使用场景?
java·c++·算法
IT_陈寒3 小时前
Vue组件复用率提升300%?这5个高阶技巧让你的代码焕然一新!
前端·人工智能·后端