切入点不处理带参数的方法

问题描述 投票:0回答:1

我添加了一个注释来跟踪执行方法所需的时间,但即使它为没有参数的方法执行,它也不会在带参数的方法上执行。

@Around("@annotation(com.x.y.a.TrackTime)")
public Object trackTime(ProceedingJoinPoint joinPoint) throws Throwable {
    return getProceedAndTrackTime(joinPoint);
}

我也尝试过使用执行:

 @Around("execution(public void com.x.y.m.myMethod(..))")

@Around("execution(public void com.x.y.m.myMethod(com.x.y.e.SomeType))")

并且

 @Around("execution(public void com.x.y.m.myMethod(..)) && args(myArgument,..)")

以上都不适用于带参数的方法,为什么会这样?应该怎么做?

aop aspectj spring-aop
1个回答
1
投票

要跟踪带或不带参数的方法的执行时间,您可以尝试下面的annotation和实现:

带注释的界面:

@Component
@Retention(RUNTIME)
@Target(ElementType.METHOD)
public @interface TimeTracker {}

并实现以上界面:

@Component
@Aspect
public class TimeTrackerAspect {

    @Around("@annotation(TimeTracker)")
    public Object around(ProceedingJoinPoint pJoinPoint) throws Throwable {
        Long startTime = System.currentTimeMillis();
        Object obj = pJoinPoint.proceed();
        System.out.println("Total milli seconds in execution of method: " + pJoinPoint.getSignature().getName() + " is :" + (System.currentTimeMillis()-startTime));
        return obj;
    }
}

然后,无论你在哪里使用注释,@TimeTracker将打印总时间,你可以从sysout更改为记录

有关详细信息,我还将示例代码推送到github,请查看:https://github.com/krishnaiitd/learningJava/tree/master/springBoot/gs-spring-boot/src/main/java/com/example/demo

© www.soinside.com 2019 - 2024. All rights reserved.