Spring Boot通过切面实现方法耗时情况
依赖
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.9.1</version></dependency>
自定义注解
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;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodExporter {
}
切面类
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;
@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;}
}
控制层
@Resourceprivate UserService userService;@PostMapping(value = "uploadFileByEasyPoi")@MethodExporter@ApiOperation(value = "通过easypoi上传文件")public AjaxResult uploadFileByEasyPoi(@RequestPart MultipartFile file){userService.uploadFileByEasyPoi(file);return AjaxResult.success();}