首页>代码>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日
最代码官方  LV167 2020年10月21日
最近浏览更多
1289755  LV3 2023年7月29日
fuyouou  LV5 2023年7月6日
wnantian 2021年6月23日
暂无贡献等级
lifuquan  LV2 2021年5月13日
你算哪块小鱼干  LV8 2021年4月22日
a198036  LV2 2021年1月22日
zdm_3366  LV3 2021年1月12日
lilu0226  LV7 2020年10月31日
wangnannan  LV4 2020年10月29日
Selchoe  LV1 2020年10月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友