取消与环绕通知的方法执行和手动执行方面这里面方法 [英] Cancel a method execution with around advice and execute this method manually inside the aspect

查看:1017
本文介绍了取消与环绕通知的方法执行和手动执行方面这里面方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能取消法的执行与各地的建议和执行它在方面的方法里面呢?

Is it possible to cancel a method's execution with around advice and execute it inside a method in the aspect?

例如:
我们有一个有一个项目:

Example : we have a project that has:

1 - 一个活动 SenderActivity.java 。这个活动里面,我们有一个方法治法()返回的字符串。

1 - An activity SenderActivity.java . Inside this activity we have a method MethodA() which returns String.

这样的:

public static String MethodA(){
String string = "a string";
return string;
}

2 - 我们赶上治法()的执行与aspect.aj内切入点

2 - We catch MethodA() 's execution with a pointcut inside the aspect.aj

这样的:

pointcut pointcut_catchMethodA() : execution(static String MethodA());

和周围的建议是:

String around() : pointcut_catchMethodA(){
//We return null or simply don't call the proceed()
return null;
}

所以在这里,每次治法()被试要执行,环绕通知取消它。

So here, everytime MethodA() is tried to be executed, around advice cancels it.

和我这个取消后想,如果我可以这样做:

And I'm wondering if after this cancellation, can I do something like :

public void MethodToCallMethodAInsideTheAspect(){

SenderActivity.MethodA();

}

因此​​,通过这种方式,我取消了原有的调用方法a()和我手动把它称为我的aspect.aj内。所以,我想治法()返回它的原始价值。 (字符串=一个字符串)。

So by this way, I cancelled the original call to MethodA() and I called it manually inside my aspect.aj. So I want MethodA() to return it's original value. (string = "a String").

这可能吗?或者,如果有实现这个的一种方式,那么请不要犹豫,你的指导。

Is it possible ? Or if there is a way to implement this, then please don't hesitate your guidance.

由于我试过这样的执行和落实我返回空值,当我想手动执行SenderActivity.MethodA()。我认为它返回around通知的价值。

Because I tried this kind of implementation and my implementation returned null value when I want to execute SenderActivity.MethodA() manually. I think it returns around advice's value.

推荐答案

我由我自己找到了解决办法:

I've found the solution by myself :

这实际上建筑工作,它必须更换只是切入点:

This architecture actually works, it has to be replaced just the pointcut :

pointcut pointcut_catchMethodA() : execution(static String MethodA());

pointcut pointcut_catchMethodA() : call(static String MethodA(..)) && !within(Aspect);

在这里,我们使用中,因为我们不希望我们的方面赶上已从方面,即拨打的电话

here, we use within because we don't want our aspect to catch calls that has been made from the aspect i.e.

public void MethodToCallMethodAInsideTheAspect(){

SenderActivity.MethodA();

}

所以这个电话将不会被环绕通知逮住。所以原来的调用 治法()已被环绕通知取消,它是由手工名为 MethodToCallMethodAInsideTheAspect()

So this call will not be catched by around advice. So the original call to MethodA() has been cancelled by around advice and it's called manually by MethodToCallMethodAInsideTheAspect()

这篇关于取消与环绕通知的方法执行和手动执行方面这里面方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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