[英]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.