JAX-RS注释接口方法上的AspectJ Pointcut调用 [英] AspectJ Pointcut call on JAX-RS annotated Interface method

查看:179
本文介绍了JAX-RS注释接口方法上的AspectJ Pointcut调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拦截以JAX-RS @POST注释的接口方法.我的切入点适用于所有非接口方法,并且@ POST-Annotation直接位于被调用的方法上.

I'm trying to intercept a method of an interface annoted with a JAX-RS @POST. My Pointcut works for all non-interface methods and if the @POST-Annotation is directly at the called method.

要拦截的接口方法:

@POST
Response postToConnector(@Context CallContext callContext, String contentStream) throws Exception;

匹配方法的切入点

@Pointcut("call(@(javax.ws.rs.DELETE || javax.ws.rs.GET || javax.ws.rs.HEAD || javax.ws.rs.OPTIONS || "
    + "javax.ws.rs.POST || javax.ws.rs.PUT) public * org.myapp..webapi..*(..))")
public void anyPublicWebApiPointcut()
{
...
}

该接口位于com.myapp.social.webapi.v1包中,即使我将方法更改为公共AspectJ也不会拦截该调用.

The interface is inside a package com.myapp.social.webapi.v1 and even if I change the method to public AspectJ will not intercept the call.

我的切入点中有什么需要更改的内容吗?我该如何运作?

Is there anything to change within my Pointcut? How can I make this working?

推荐答案

顾名思义,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.

这篇关于JAX-RS注释接口方法上的AspectJ Pointcut调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆