0

I have a aspect defined (in a spring project) which is supposed to be called after a method is executed.

class Crap {
   public void blah(){
       //Do Something here
   }

   public void blah_internal(){
       blah();
       //Do Something else here
   }
}

class MyAspect {
   @After("execution(* Crap.blah(..))")
   public void doSomething(JoinPoint joinPoint) {
      System.out.println("Coming into the aspect");
      //Some logic here
   }
}

Now if I call Crap.blah method normally, then the aspect works

new Crap().blah(); //Prints "Coming into the aspect"

But if I call the method using another method, it doesn't work

new Crap().blah_internal();   //Prints nothing

What is going wrong here?

Vivek
  • 1,640
  • 1
  • 17
  • 34
  • I think you should use call() instead of execution(). how? – Ye Win Dec 24 '14 at 10:29
  • Modified the question, it doesn't seems to be the problem with the threading. It seems that it is not getting triggered when the pointcut is called by method of the same class – Vivek Dec 24 '14 at 10:49
  • See here for an explanation: http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies – Alan Hay Dec 24 '14 at 11:29

2 Answers2

0

Use your pointcut definition with additional asterix

@After("execution(* Crap.blah*(..))")
sven.kwiotek
  • 1,459
  • 15
  • 22
0

Are you using Spring's own implementation of AOP? This is a known limitation, aspects aren't applied when calling a method within the same class.

You can switch to AspectJ by putting <aop:aspectj-autoproxy /> in your xml files (and importing required aspectj dependencies obviously). Aspectj works in the scenario you've depicted.

M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • The autoproxy tag is for Spring AOP which, as you just explained, does not work for internal calls because it is proxy-based and calls to methods of `this` are not routed through a proxy. What you need is `` instead, I guess. – kriegaex Dec 24 '14 at 13:40