我能够从建议的方法调用中获取签名和参数,但我无法弄清楚如何获取返回值或异常。我有点假设它可以以某种方式使用 around 并继续来完成。
问问题
28605 次
3 回答
9
您还可以在返回通知后使用返回值。
package com.eos.poc.test;
public class AOPDemo {
public static void main(String[] args) {
AOPDemo demo = new AOPDemo();
String result= demo.append("Eclipse", " aspectJ");
}
public String append(String s1, String s2) {
System.out.println("Executing append method..");
return s1 + s2;
}
}
获取返回值的定义方面:
public aspect DemoAspect {
pointcut callDemoAspectPointCut():
call(* com.eos.poc.test.AOPDemo.append(*,*));
after() returning(Object r) :callDemoAspectPointCut(){
System.out.println("Return value: "+r.toString()); // getting return value
}
于 2014-04-08T11:44:59.403 回答
7
使用around()
通知,您可以通过 using 获取被拦截的方法调用的返回值proceed()
。如果您愿意,您甚至可以更改方法返回的值。
例如,假设您m()
在 class中有一个方法MyClass
:
public class MyClass {
int m() {
return 2;
}
}
假设您在自己的 .aj 文件中有以下方面:
public aspect mAspect {
pointcut mexec() : execution(* m(..));
int around() : mexec() {
// use proceed() to do the computation of the original method
int original_return_value = proceed();
// change the return value of m()
return original_return_value * 100;
}
}
于 2013-06-17T09:53:15.477 回答