我的应用程序中编码了几个方面。除以下内容外,所有其他内容均有效。
服务接口
package com.enbiso.proj.estudo.system.service;
...
public interface MessageService {
...
Message reply(Message message);
Message send(Message message);
...
}
服务实施
package com.enbiso.proj.estudo.system.service.impl;
....
@Service("messageService")
public class MessageServiceImpl implements MessageService {
...
@Override
public Message reply(Message message) {
...
return this.send(message);
}
@Override
public Message send(Message message) {
...
}
}
Aspect
@Aspect
@Component
public class NewMessageAspect {
...
@AfterReturning(value = "execution(* com.enbiso.proj.estudo.system.service.impl.MessageServiceImpl.send(..))",
returning = "message")
public void perform(Message message){
...
}
}
当我尝试执行send
方法调试点在方面没有被击中perform
.
UPDATE
我做了一些调查,发现这不起作用,当send
方法是从调用reply
方法如下
@Autowire MessageService messageService;
...
messageService.reply(message);
但是如果我调用该方法messageService.send(message)
效果很好。但是由于reply方法在内部调用send方法,它不应该也调用aspect吗?
我不知道我做错了什么。请帮我。
谢谢jst为了清理事情。只是为了给未来的开发人员提供信息,我发布了这个问题的完整答案
Lets assume that there is a bean from
SimplePojo
public class SimplePojo implements Pojo {
public void foo() {
this.bar();
}
public void bar() {
...
}
}
当我们调用该方法时foo()
,它重新调用该方法bar()
在里面。甚至想到了方法foo()
从 AOP 代理调用,内部调用bar()
不属于 AOP 代理的范围。
所以最终这使得,如果该方法有任何建议的话bar()
不被调用
Solution
Use AopContext.currentProxy()
来调用该方法。不幸的是,这将逻辑与 AOP 耦合在一起。
public void foo() {
((Pojo) AopContext.currentProxy()).bar();
}
参考:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)