我有一个 @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() 内部之前返回的 token ?
最佳答案
不,你不能,因为你想做的事情没有任何逻辑意义:
- 顾名思义,
@Before
建议在执行目标方法之前运行。 - 但方法执行前不能有返回值,只有执行后才有返回值。
- 因此,您只能处理
@After
或@Around
建议中的返回值,在后一种情况下,您将得到它作为的结果继续()
。 - 您的代码暗示您想要“使用之前返回的 token ”。但是 @Before 建议有一个 void 返回类型,即它不返回任何内容。那么你想用什么?
如果您需要一些示例代码,请告诉我。
关于java - AspectJ 有没有办法从 Before 检索返回值并在切入点内使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32403919/