你不能用代理人这样做,但如果你注意到这种类型,而不是地方变量,那么通过人工编码进行编织会让你去。 (我不认为当地可变准入是一个要点。) 不管怎么说,这里有一些法典。
说明:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Later {}
具有这一说明的一类:
package com.dummy.aspectj;
@Later
public class HeavyObject{
public HeavyObject(){
System.out.println("Boy, I am heavy");
}
}
主要类别:
package com.dummy.aspectj;
public class HeavyLifter{
public static void main(final String[] args){
final HeavyObject fatman = new HeavyObject();
System.out.println("Finished with main");
}
}
以及一个方面:
package com.dummy.aspectj;
public aspect LaterAspect{
pointcut laterInstantiation() :
execution(@Later *.new(..)) ;
void around() : laterInstantiation() {
new Thread(new Runnable(){
@Override
public void run(){
System.out.println("Wait... this is too heavy");
try{
Thread.sleep(2000);
} catch(final InterruptedException e){
throw new IllegalStateException(e);
}
System.out.println("OK, now I am up to the task");
proceed();
}
}).start();
}
}
这里指的是作为AspectJ/Java申请方从ec子中转来的重型设备的产出:
Finished with main
Wait... this is too heavy
OK, now I am up to the task
Boy, I am heavy