使用 AspectJ 的新手:无法调用方面
因此,我开始考虑在域中对象的状态发生变化时使用 AspectJ 来处理处理事件。
实际上,我想编写包含我域中所有 setter 方法的建议。当通知被调用时,它将检查正在设置的字段的初始值,运行setter,然后在setter执行后检查值。如果值发生变化,它将向事件侦听器触发一个事件,通知发生的变化。
我使用了这里找到的教程: http://www.andrewewhite .net/wordpress/2010/03/17/aspectj-annotation-tutorial/,但我无法接到任何建议电话。请注意,我仅使用 LTW 方法来编写建议,而不是使用 AspectJ 语言编写建议并对其进行预编译。
我的 aop.xml (在我的测试套件的 META-INF 中)如下所示:
<aspectj> <aspects> <aspect name="domain.aop.TestAspect"/> </aspects> </aspectj>
我创建的 Aspect 类如下所示:
package domain.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class TestAspect { @Around("call(* domain.*.set*(..))") public void aroundSetMethods (JoinPoint jp) { System.out.println ("aroundSetMethod called"); } }
当我运行测试用例时,我可以看到(通过设置断点)该方法(domain.Error.setTask ()) 被调用。我相信这应该触发我的建议,但我从未进入建议方法。
有什么指示可以指出我在这里做错了什么吗?
谢谢
So, I've started looking into using AspectJ to handle processing events when the state of an object in my domain changes.
Effectively, I'd like to write advice that wraps all of the setter methods in my domain. When the advice is called, it'll check the initial value of the field being set, run the setter, then check the value after the setter executes. If the value changes, it'll fire an event to an event listener, informing of the change.
I used the tutorial found here: http://www.andrewewhite.net/wordpress/2010/03/17/aspectj-annotation-tutorial/, but I'm unable to get any advice calls to be made. Notice that I'm only using the LTW method for weaving my advice, I'm not writing advice using the AspectJ language and precompiling it.
My aop.xml (in the META-INF for my test suite) looks like this:
<aspectj> <aspects> <aspect name="domain.aop.TestAspect"/> </aspects> </aspectj>
The Aspect class that I've created looks like this:
package domain.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class TestAspect { @Around("call(* domain.*.set*(..))") public void aroundSetMethods (JoinPoint jp) { System.out.println ("aroundSetMethod called"); } }
When I run my test case, I can see (by setting a breakpoint) that a method (domain.Error.setTask ()) gets called. It is my belief that this should trigger my advice, but I never get into the advice method.
Any pointers to what I'm doing wrong here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还必须包括要编织哪些类。
尝试用以下内容替换您的 aop.xml 文件:
如果您正确配置了运行时服务器或 Java 代理,您还可以在日志中看到编织过程。
我希望它有帮助!
You must also include which classes you want to weave.
Try to replace your aop.xml file with this:
If you have configured your runtime server or Java agent correctly, you can also see the weaving process in your log.
I hope it helps!