由于技术限制,没有around()
建议initialization()
或preinitialization()
切入点之类的东西。进入和退出相应连接点的时间顺序还有另一个问题。看这个例子:
public abstract class ApplicationBase {
private int id = 0;
public ApplicationBase(int id) {
this.id = id;
}
}
public class Application extends ApplicationBase {
private String name = "<unnamed>";
public Application(int id, String name) {
super(id);
this.name = name;
}
public static void main(String[] args) {
new Application(1, "Foo");
new Application(2, "Bar");
}
}
public aspect ExecutionTimingAspect {
private String indentText = "";
pointcut constructorCall() :
call(*Application*.new(..));
pointcut constructorRelated() :
constructorCall() ||
initialization(*Application*.new(..)) ||
preinitialization(*Application*.new(..)) ||
execution(*Application*.new(..));
after() : constructorRelated() {
indentText = indentText.substring(2);
System.out.println(indentText + "<< " + thisJoinPointStaticPart);
}
before() : constructorRelated() {
System.out.println(indentText + ">> " + thisJoinPointStaticPart);
indentText += " ";
}
Object around() : constructorCall() {
long startTime = System.nanoTime();
Object result = proceed();
System.out.println(indentText + "Constructor runtime = " + (System.nanoTime() - startTime) / 1.0e9 + " s\n");
return result;
}
}
您将看到以下输出:
>> call(Application(int, String))
>> preinitialization(Application(int, String))
<< preinitialization(Application(int, String))
>> preinitialization(ApplicationBase(int))
<< preinitialization(ApplicationBase(int))
>> initialization(ApplicationBase(int))
>> execution(ApplicationBase(int))
<< execution(ApplicationBase(int))
<< initialization(ApplicationBase(int))
>> initialization(Application(int, String))
>> execution(Application(int, String))
<< execution(Application(int, String))
<< initialization(Application(int, String))
<< call(Application(int, String))
Constructor runtime = 0.00123172 s
>> call(Application(int, String))
>> preinitialization(Application(int, String))
<< preinitialization(Application(int, String))
>> preinitialization(ApplicationBase(int))
<< preinitialization(ApplicationBase(int))
>> initialization(ApplicationBase(int))
>> execution(ApplicationBase(int))
<< execution(ApplicationBase(int))
<< initialization(ApplicationBase(int))
>> initialization(Application(int, String))
>> execution(Application(int, String))
<< execution(Application(int, String))
<< initialization(Application(int, String))
<< call(Application(int, String))
Constructor runtime = 0.00103393 s
你能看到派生类的预初始化是如何在其基类的预初始化之前开始和结束的吗?以及初始化如何以相反的方式工作,但是作为一个额外的复杂构造函数执行嵌入在初始化中?
也许现在您明白了,即使可以通过 测量初始化,around()
也不会反映构造函数的整体执行时间。因此,如果您足够幸运能够拦截构造函数call()
而不是execution()
因为您可以访问调用代码,那么您很好,甚至可以around()
像我在示例中所做的那样使用(顺便说一下,这不是线程安全的,但是我尽量保持简单)。如果您不能影响调用者,但只能编织被调用者,则需要使用其他技巧,例如在进入某个构造函数的预初始化时通过方面内部记账,before()
然后在退出相同构造函数调用的初始化时通过after()
. 即,您需要在建议执行之间保持一些内部状态。这是可能的,但有点复杂。如果您想进一步讨论这个问题,请告诉我。