I have several Aspects coded in my application. All others works except for the following.
Service Interface
package com.enbiso.proj.estudo.system.service;
...
public interface MessageService {
...
Message reply(Message message);
Message send(Message message);
...
}
Service Implementation
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){
...
}
}
When I try to execute the send
method the debug point is not getting hit in the aspect perform
.
UPDATE
I did some investigations and found that this doesn't work, when the send
method is invoked from the reply
method as below
@Autowire MessageService messageService;
...
messageService.reply(message);
But if I call the method messageService.send(message)
it works fine. But as reply method is calling send method internally, shouldn't it also invoke the aspect?
I have no idea what i have done wrong. Please help me.
You are running into a limitation of Spring AOP on self-invocation. You basically can get around it by using AopContext.currentProxy(), refactor code into different beans, or use full ApsectJ weaving.
See explanation here and workaround(s). http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies
I guess the problem is the
@args
condition.Spring documentation states the following:
Therefore the parameter of
@args
has to be a type expression. So the correct pointcut expression isor simply
Please adjust the package of
Message
if it doesn't fit.Thank you jst for clearing the things up. Just for the information purposes for the future developer in SO, I'm posting the full answer to this question
Lets assume that there is a bean from
SimplePojo
When we call the method
foo()
, it reinvokes the methodbar()
inside it. Even thought the methodfoo()
is invoked from the AOP Proxy, the internal invocation of thebar()
is not covered by the AOP Proxy.Solution
Use
AopContext.currentProxy()
to call the method. Unfortunately this couples the logic with AOP.Reference:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies