简体   繁体   English

Spring Boot和AspectJ —我可以拦截对java.util.ArrayList.size()方法的调用吗?

[英]Spring Boot and AspectJ — Can I intercept a call to the java.util.ArrayList.size() method?

I'm learning about AOP using Spring Boot, and I feel I have a good handle on the concept. 我正在使用Spring Boot学习有关AOP的知识,并且我对这个概念有很好的了解。 I'm able to intercept calls to methods I've created, but what about methods found within other libraries (such as "java.util.ArrayList.size()"). 我可以拦截对已创建方法的调用,但是在其他库(例如“ java.util.ArrayList.size()”)中找到的方法呢? Can calls to these methods be intercepted too? 也可以拦截对这些方法的调用吗? If so, what would the aspect look like? 如果是这样,方面将是什么样? I've tried the following aspect declaration, but I'm probably missing something here: 我已经尝试了以下方面声明,但是这里可能遗漏了一些内容:

@Around(value = "execution(int java.util.ArrayList.size(..))")
    public void aroundListSize(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Before the size method is being invoked.");

        joinPoint.proceed();

        System.out.println("After the size method was invoked.");
    }

Spring AOP works with creating proxies around the advised classes. Spring AOP围绕建议的类创建代理。 This has many limitations when compared to native AspectJ that directly modifies the bytecode of the advised classes. 与直接修改建议类的字节码的本机AspectJ相比,这有很多限制。 One of these limitations (among many others) is it only works with Spring managed beans, not any Java objects , like when using native AspectJ. 这些限制(其中包括许多限制)之一是, 它仅适用于Spring托管的bean,不适用于任何Java对象 ,例如使用本机AspectJ时。

If your ArrayList objects are Spring managed beans, it will work to some extent, but you also need to be aware that the proxy based AOP mechanism only works through the advised class' external interface, so it will only work when invoked through the Spring provided proxy classes, and not when invoked through the this reference. 如果您的ArrayList对象是Spring托管的bean,那么它在某种程度上会起作用,但是您还需要注意,基于代理的AOP机制仅通过建议类的外部接口起作用,因此只有在通过提供的Spring调用时才起作用代理类,而不是通过this引用调用时。 So invoking this.method() from an advised class' anotherMethod() , it will not go through the proxy instance and the advice code will not be executed. 因此,从建议的类的anotherMethod()调用this.method()不会通过代理实例,并且不会执行建议代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference - Android Studio Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference Saving Fragment State在null对象引用上给出错误:int java.util.ArrayList.size()' - Saving Fragment State gives error: int java.util.ArrayList.size()' on a null object reference Spring Boot JPA java.lang.IndexOutOfBoundsException:索引:1,大小:1 在 java.util.ArrayList.rangeCheck - Spring boot JPA java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck 如何拦截Java或Android中对库类的方法调用? - How can I intercept a method call to a library class in Java or Android? 如何使用 Spring AOP 或 AspectJ 拦截给定方法中的每个方法调用 - How to intercept each method call within given method using Spring AOP or AspectJ 春季启动:添加@Transactional会产生java.lang.ClassNotFoundException:org.aspectj.util.PartialOrder $ PartialComparable - Spring Boot: Adding @Transactional produces java.lang.ClassNotFoundException: org.aspectj.util.PartialOrder$PartialComparable 在 Spring Boot 中使用本机 AspectJ 拦截嵌套方法 - Intercept nested methods using native AspectJ in Spring Boot Spring 启动 java.util.concurrent.ThreadPoolExecutor 大小 - Spring Boot java.util.concurrent.ThreadPoolExecutor size 方法参数aspectj无法正常工作 - method parameter aspectj is not working spring boot 如何使用标准java功能拦截方法调用(没有AspectJ等)? - How do I intercept a method invocation with standard java features (no AspectJ etc)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM