AspectJ有没有办法从Before检索返回值并在切入点中使用它?

时间:2015-09-04 17:52:40

标签: java annotations aspectj pointcut

我有一个@Before方法,它返回一些我希望在切入点中使用的标记。

@Pointcut("execution(getData())")
    private void selectAll(){}

@Before("selectAll()")
public void beforeAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
    //Return the token
}
public void getData(){
     //Is there a way I can use the token returned by before??
}
@After("selectAll()")
public void afterAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
     //destroy the token
}

有没有办法可以在getData()内部使用之前返回的标记?

1 个答案:

答案 0 :(得分:1)

不,你不能,因为你想做的事情没有任何逻辑意义:

  • 顾名思义,@Before建议在执行目标方法之前运行
  • 但是在执行某个方法之前,只有在执行该方法之后才能有返回值。
  • 因此,您只能处理@After@Around建议中的返回值,而后一种情况则是proceed()的结果。
  • 您的代码暗示您希望"使用之前返回的令牌"。但@Before建议有一个void返回类型,即它不返回任何内容。那么你想用什么?

如果您需要一些示例代码,请与我们联系。