0

I have an @Before method which is returning some token that I wish to use inside the pointcut.

@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
}

Is there a way I can use the token returned by before inside getData()?

as3rdaccount
  • 3,711
  • 12
  • 42
  • 62

1 Answers1

1

No, you cannot, because what you want to do does not make any logical sense:

  • As the name implies, the @Before advice runs before the target method is executed.
  • But before a method is executed, there cannot be a return value, only after it is executed.
  • Thus, you can only handle a return value in an @After or @Around advice, and in the latter case you get it as the result of proceed().
  • Your code implies you want to "use the token returned by before". But the @Before advice has a void return type, i.e. it returns nothing. So what do you want to use?

Please let me know if you need some sample code.

kriegaex
  • 63,017
  • 15
  • 111
  • 202