带有局部变量注释的 AOP
我想使用局部变量注解来做更好的AOP。一个想法是实现Future
@NonBlocking ExpensiveObject exp = new ExpensiveObject();
//returns immediately, but has threaded out instantiation of the ExpensiveObject.
exp.doStuff();
//okay, now it blocks until it's finished instantiating and then executes #doStuff
我能否以某种方式让 AspectJ 对此感到厌烦并通过局部变量注释完成我想要的工作?我知道其他线程已经表明 Java 并不真正支持它们,但这将是神奇的。我真的不想传递 Future 并破坏封装。
I want to use local variable annotations to do better AOP. One idea is to implement the Future<T> concept with a proxy using an annotation.
@NonBlocking ExpensiveObject exp = new ExpensiveObject();
//returns immediately, but has threaded out instantiation of the ExpensiveObject.
exp.doStuff();
//okay, now it blocks until it's finished instantiating and then executes #doStuff
Can I sick AspectJ on this somehow and get what I want done with local variable annotations? I know other threads have indicated that Java doesn't really support them but it would be magical. I really don't want to pass around a Future and break encapsulation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用代理来执行此操作,但如果您注释类型而不是局部变量,则真正的aspectj字节码编织将帮助您实现这一点。 (我不认为局部变量访问被支持作为切入点)。无论如何,这是一些代码。
注释:
标有此注释的类:
主类:
和方面:
以下是当您从 eclipse 中将 HeavyLifter 作为 AspectJ/Java 应用程序运行时的输出:
You can not do this with a proxy, but real aspectj bytecode weaving will get you there if you annotate the type instead of the local variable. (I don't think local variable access is supported as a pointcut). Anyway, here's some code.
An annotation:
A class marked with this annotation:
A main class:
and an aspect:
Here is the output from HeavyLifter when you run it as an AspectJ/Java Application from eclipse: