如何为同一类中的另一个方法调用的方法运行方面建议
我对 Spring AOP 有疑问。我正在尝试使用方面触发一个方法,但是触发该方面的方法也是同一个类的方法,并且方面不起作用(顺便说一句没有错误)。像这样
class A extends Runnable{
public void write(){
System.out.println('Hi');
}
public void run(){
this.write();
}
}
<aop:after-returning method="anyMethod" pointcut="execution(* A.write(..))"/>
任何想法将不胜感激
谢谢
I am having a throuble about Spring AOP. I am trying to trigger a method using aspect but the method that will trigger the aspect is also the method of the same class and aspect is not working(No errors by the way).Like this
class A extends Runnable{
public void write(){
System.out.println('Hi');
}
public void run(){
this.write();
}
}
<aop:after-returning method="anyMethod" pointcut="execution(* A.write(..))"/>
Any ideas will be appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,在不同的线程中调用建议的方法没有任何区别。只需确保传递给线程的实例是由 spring 应用程序上下文创建的,而不是由您的应用程序代码创建的。
另外,由于您建议在类中声明方法,而不是接口 -
write()
- 您需要执行加载时编织(并在类路径中包含 cglib)。The fact that the advised method is called in a different thread doesn't make any difference. Just make sure the instance that you pass to the thread is created by the spring application context and not by your application code.
Also, since you're advising a method declared in a class, not an interface --
write()
-- you'll need to perform load-time weaving (and have cglib in your classpath).这是因为 Spring AOP 是基于代理的。您可以使用代理将调用委托给底层对象。但是,当底层对象的方法调用其内部同一类(您的用例)的另一个方法时,代理不会出现,因此您想要实现的目标是不可能的。有一些解决方法,但它们破坏了 AOP 的真正目的。
您可以在这里参考更多信息。
http ://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies
This is because Spring AOP is proxy based. You use a proxy to delegate calls to the underlying object. However, when an underlying object's method makes a call to another method inside it, of the same class (your use case) then proxy does not come into picture and hence what you are trying to achieve is not possible. There are some work arounds, but they kill the very purpose of AOP.
You can refer more information here.
http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies
正如 Abhishek Chauhan 所说,Spring AOP 是基于代理的,因此无法拦截对 this.someMethod() 的直接调用。但好消息是,您还可以通过加载时编织在 Spring 应用程序中使用成熟的 AspectJ,如 Spring 手册。这样您就可以摆脱限制,甚至摆脱整个代理开销,因为 AspectJ 不需要任何代理。
As Abhishek Chauhan said, Spring AOP is proxy-based and thus cannot intercept direct calls to
this.someMethod()
. But the good news is that you can also use full-blown AspectJ within Spring applications via load-time weaving as described in the Spring manual. This way you can get rid of the limitation and even of the whole proxy overhead because AspectJ does not need any proxies.