由于技术限制,没有关于initialization()或preinitialization()切入点的around()建议.并且在输入和退出相应的连接点时的时间顺序存在另一个问题.看看这个例子:
public abstract class ApplicationBase {
private int id = 0;
public ApplicationBase(int id) {
this.id = id;
}
}
public class Application extends ApplicationBase {
private String name = "";
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
");
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()进入某个构造函数的预初始化时的方面内部簿记,然后在初始化相同的构造函数调用时在()之后退出.即你需要在建议执行之间保持一些内部状态.这是可能的,但有点复杂.如果您想进一步讨论,请告诉我.