自定义AOP,实现自动捕获方法的执行时间

1.自定义Aspect

@Component
@Aspect
public class JournalServiceAspect {

    private static final Logger logger = LoggerFactory.getLogger(JournalServiceAspect.class);

    private final String POINT_CUT = "execution(* delta.config.controller..*(..))";

    @Pointcut(POINT_CUT)
    private void pointcut(){}

    private ThreadLocal<Long> beginTime = new ThreadLocal<>();

    @Before(value = POINT_CUT)
    public void before(JoinPoint joinPoint){
//        logger.info("前置通知");
        beginTime.set(System.currentTimeMillis());
    }

    @After(value = POINT_CUT)
    public void doAfterAdvice(JoinPoint joinPoint){
        String className = joinPoint.getSignature().getDeclaringType().getName();
        String methodName = joinPoint.getSignature().getName();
        logger.info("捕获 {}#{} cost time : {}ms",className,methodName,System.currentTimeMillis() - beginTime.get());
    }
}

2.springboot aop的execution表达式详解

切入点表达式说明

* delta.config.controller..*(..)

表达式说明

表达式说明
*:表示返回类型,*表示所有的类型
delta.config.controller:表示要拦截的包名,此处设置controller
..:表示包、子孙包下所有类的所有方法
*(..):其中*表示方法名,表示所有方法,后面括弧里面表示方法的参数,..表示任何参数
posted @   SpecialSpeculator  阅读(92)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示