0

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

Neron
  • 1,500
  • 7
  • 30
  • 52

3 Answers3

1

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).

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • Thanks for your answer. I figured that, I wrote the code wrongly. Check ne new one above pls. As u can see the new one, the thread is calling the method belongs to itself. SO this means, there is no interaction with spring context in this operation because it calls it's own method. How can I do this now? – Neron Jan 06 '12 at 11:33
  • @user1072848 It's not a matter of who calls it, rather how it's instantiated. – Dave Newton Jan 06 '12 at 11:49
  • So what should I do to create it like u said? – Neron Jan 06 '12 at 11:55
  • Do not use `new` to instantiate it. Declare your bean in the application context and inject it or retrieve it programatically from the context. Spring is only able to apply advises to instances that it creates itself. – Costi Ciudatu Jan 06 '12 at 23:09
  • I got my instances from applicationContext but it did not work. I read load time weaving but I thought that it should not be some much configuration for Spring because this is what Spring is: simplicity. I really do not get it why it does not work – Neron Jan 09 '12 at 09:21
  • Maybe this will sound dumb... but have you double-checked the pointcut expression ? – Costi Ciudatu Jan 09 '12 at 15:57
  • I did but I also called another method of another class but it worked – Neron Jan 10 '12 at 12:37
0

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
  • 309
  • 5
  • 15
0

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202