Spring AOP - 方面循环执行
首先,我需要说我是 Spring AOP 的新手(好吧,我根本就是 AOP 的新手)。 在我的应用程序中,我有方面建议的服务类,直到这一点一切都很好。方面被触发,一切正常。但我需要从我的角度调用该服务方法,并且存在问题。我的方面(逻辑上)针对每次调用而触发,并且每次都因 StackOwerflow 错误而结束。
可以防止该方面循环吗?
我有想法创建 IAspectSandbox 接口(或类),并且从实现该接口的类中调用方法不会触发方面。但我真的不知道如何实现这个目标:)
我的类架构:
@Service
public class MyService
{
public BarObject update( FooObject item )
{
BarObject barObject = new BarObject();
// save FooObject to database and detect changes against old row
// information about fields, that was changed is in BarObject
return barObject;
}
}
--------------------------------------------------------------------------
@Aspect
public class MyServicePointcut
{
@Pointcut("execution(* cz.package.service.MyService.update(..))")
public void myServiceItemChanged() {}
}
--------------------------------------------------------------------------
@Component
@Aspect
public class PraceZadaniChangeAspect
{
@AutoWire
private MyService myService;
@AfterReturning("cz.package.pointcuts.MyServicePointcut.myServiceItemChanged()", returning = "returnVal")
public void execute( BarObject returnVal )
{
// do something with BarObject ... mostly check changes
// .....
// .....
// at the end I need to save changes
myService.update( returnVal.getFooObject() ); // after this call is this aspect triggered again. I know why, but I don't want to :)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案#1:仅调用建议方法(围绕建议)
如果您将服务自动装配回您的方面,您仍然会调用 Spring 的代理机制,包括您已应用于服务的 AOP 方面。
请参阅Spring AOP章节中的“周围建议”:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj-around-advice
基本上,做像这样的事情:
我对代码不是 100% 确定,但是
proceed()
应该直接调用目标方法,而不递归调用 AOP 代理。答案#2:调用多个目标对象方法
如果您需要从方面内的服务对象调用多个方法,则需要通过
getTarget()
:Answer #1: Calling Advised Method Only (Around Advice)
If you autowire your service back into your aspect, you're still invoking Spring's proxy mechanism, including the AOP aspect that you've applied to your service.
See "Around Advice" in the Spring AOP chapter:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj-around-advice
Basically, do something like this:
I am not 100% sure on the code, but
proceed()
should call the target method directly without invoking the AOP proxy recursively.Answer #2: Calling Multiple Target Object Methods
If you need to call multiple methods from that service object within your aspect, you'll need access to the unproxied object via
getTarget()
: