aop—如何在java中实现jar中类的方法的方面

tv6aics1  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(214)

我想测量客户端库中存在的方法的执行时间。我的应用程序是一个spring启动应用程序,我正在调用一个来自某个库的类中的方法。我试着运行下面的代码,但它不工作。在我的例子中,客户机类对象不是由spring容器管理的,这可能是一个原因,但是否有任何方法可以获得该方法。

@Aspect
@Component
public class LoggingAspect {
    public static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

    @Around("execution(public * Invokers.ApiClient.execute(..))") //okhttp3.Call,
    public Object methodTimeLogger(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();

        // Get intercepted method details
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();

        // Measure method execution time
        StopWatch stopWatch = new StopWatch(className + "->" + methodName);
        stopWatch.start(methodName);
        Object result = proceedingJoinPoint.proceed();
        stopWatch.stop();
        System.out.println("----------Start----------");
        // Log method execution time
        if (logger.isInfoEnabled()) {
            logger.info(stopWatch.prettyPrint());
        }
        System.out.println("----------End----------");
        return result;
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题