0
votes

Spring AOP works well on methods that exposed via an interface. Spring AOP provides an option on proxy via target class @EnableAspectJAutoProxy(proxyTargetClass=true)

In this case the target class is the proxy, so I assume all of its methods - public, protected and private.

   interface ISample {
      public method1();
   }

class Sample implements ISample { 
   @LogMe
   public method1() {
     ...
     method2();
   }

   @LogMe
   private method2() {
     ...
   }
}

I have cglib library configured in the class path, the config class has @EnableAspectJAutoProxy(proxyTargetClass=true) and the aspect class has @Aspect and @Component. The aspect class logs all method calls if it is annotated with @LogMe.

Question is with this setting method2() call does not get logged? If the proxy is on the target class, should'nt this work?

1
CGLIB proxy works by sub-classing the target. As the method2() has private access modifier it can't be overridden and hence can't be proxied; which is what you observe. Moreover the method2() is invoked inline and not via a spring managed bean which could be intercepted. - Bond - Java Bond
@Bond-JavaBond thanks! the explanation from spring docs makes it clear. Do have a suggested link where I can refer for load time weaving of aspectj using spring? With the basic configuration I did by adding EnableLoadTimeWeaving and EnableSpringConfigured to the config file and start the server with spring-instruments jar the weaving does not seem to work. A working set of steps would be helpful to refer. - Bhargavi Krishnamachari
please compile the problems faced as new post / question .. will be glad to help :) - Bond - Java Bond

1 Answers

0
votes

CGLIB proxy did not work for my case. But I used AspectJ loadtime weaving.

More details in thread Spring AspectJ loadtimeweaving not invoked