首页>代码>Spring Boot项目中实现最简单的自定义注解实例>/spring-aop-example/src/main/java/com/hellokoding/spring/LoggingAspect.java
package com.hellokoding.spring; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { // @Pointcut("execution(* commandLineRunner(..))") // private void commandLineRunner() {} @Around("@annotation(logExecutionTime)") //@Around("execution(* commandLineRunner(..))") // @Around("commandLineRunner()") public Object logExecutionTime(ProceedingJoinPoint joinPoint, LogExecutionTime logExecutionTime) throws Throwable { long start = System.currentTimeMillis(); Object proceed = joinPoint.proceed(); long executionTime = System.currentTimeMillis() - start; System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms by " + logExecutionTime.name()); return proceed; } }

xb1406112453 LV5
2020年10月21日
最代码官方 LV168
2020年10月21日