微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java - 如何在java中围绕jar中的类的方法实现方面

如何解决java - 如何在java中围绕jar中的类的方法实现方面

我想测量客户端库中存在的方法的执行时间。我的应用程序是一个 Spring Boot 应用程序,我正在调用来自某个库的类中存在的方法。我尝试运行下面的代码,但它不起作用。在我的情况下,客户端类对象不是由 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;
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。