且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JAX-RS注释接口方法上的AspectJ Pointcut调用

更新时间:2023-11-18 08:18:34

顾名思义,call()切入点的作用是拦截对特定方法/构造函数的调用.为了使其正常工作,调用者(即呼叫所在的代码段)必须在您的控制之下,即它必须是编织的.所以如果您已经编织了org.myapp..webapi..*类,并且调用也已从那里发出,它应该可以工作.它不起作用使我假设POST调用来自编织代码之外的某个地方,例如JRE或第三方库.

What a call() pointcut does is, as the name implies, intercept calls to a certain method/constructor. In order for this to work, the caller (i.e. the piece of code where the call is located) must be under your control, i.e. it must have been woven. So if e.g. you have woven the org.myapp..webapi..* classes and the call has also been issued from there, it should work. That it does not work makes me assume that the POST calls come from somewhere outside the woven code, e.g. the JRE or a 3rd party library.

因此,如果org.myapp..webapi..*在您的控制之下,即可以将方面代码编织到其中,则应使用execution()切入点.与call()相比,它被编织到被调用者中,即被编织到定义该方法的代码中,而不是被编织到被调用该方法的许多地方.这样,您可以拦截所有方法执行,无论它们来自应用程序内部还是来自第三方或JRE代码.它甚至适用于由反射触发的方法执行.

So if org.myapp..webapi..* is under your control, i.e. you can weave aspect code into it, you should use an execution() pointcut. In contrast to call() it is woven into the callee, i.e. into the code where the method is defined, not into the many places where it is called. This way you can intercept all method executions, regardless if they come from within your application or 3rd party or JRE code. It will even work for method executions triggered by reflection.

call()execution()具有根本不同的语义,值得学习和理解.根据经验,应尽可能使用execution(),即只要被呼叫方可为您编织.如果您无法进入被叫方并且必须使用呼叫者,则call()只是您的后备.如果出于某种原因需要基于连接点上下文做出任何决定,例如call()也可能有意义.在around()建议中,该建议根据某些条件决定是否调用原始方法.

call() and execution() have fundamentally different semantics which pay off to learn and understand. As a rule of thumb, you should try to use execution() whenever possible, i.e. whenever the callee is weavable for you. call() is just your fall-back if you cannot weave into the callee and must use the caller. call() can also make sense if for some reason you need to make any decisions based on the joinpoint context, e.g. in an around() advice which decides to call or not to call the original method based on some condition.