Partha
Partha

Reputation: 310

Spring AOP - Point Cut not getting called

I have a SpringBoot Application. I have defined an Annotation say "Track", and I have annotated few methods in different packages which I want aop to consider. The annotation has been defined as below :

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Track {

}

I have not missed the @EnableAspectJAutoProxy in the @Configuration class of my package.

I have a Pointcut and an Advice defined in the Aspect like below :

@Aspect
@Component
public class MyAspect {

@Pointcut("execution(@Track * *.*(..))")
void annotatedMethod() {
    // No Implementation required
}

@Around("annotatedMethod() && @annotation(methodLevelTrack)")
public void adviseAnnotatedMethods(ProceedingJoinPoint proceedingJoinPoint,
        Track methodLevelTrack) throws Throwable {

     // do some task
    proceedingJoinPoint.proceed();
    // do some task after the method is executed.
  }
}

My intention is: for any method (annotated with @Track) in any package, with any access modifier, and any number of input arguments, and any return type, to follow the aspect's @Around advice.

Now, the interesting situation is as below : I have a class say "Engine" which calls other classes and downstream systems to perform a long-running operation. Let's define the class as follows :

public class Engine {
  // bunch of other autowired objects

 public void processTask() {
   <autowired_object_A>.someMethod() // this method has been annotated with @Track
   <autowired_object_B>.someMethod() // this method has also been annotated with @ Track
   .... // bunch of other methods in other autowired objects that have been annotated with @ Track

   someMethodOfEngineClass(); // Now this has been defined in the Engine class as below, but pointcut doesn't recognize this method!

 }

 @Track
 private void someMethodOfEngineClass() {
  // do something
 }
}

All the "other" autowired objects' methods are getting recognized by pointcut as expected but the method within this Engine class, that has been annotated with @Track, is not recognized. What's the mystery?

I have tried making "someMethodOfEngineClass" method public, return something instead of void and all those combinations and it doesn't work.

What am I missing? Is it the pointcut definition expression? I have defined the aspect in one of the sub packages, is aspect supposed to be defined at the top level in the package structure?

Can you folks please suggest something that can work? I am kinda stuck at this.

Upvotes: 2

Views: 3088

Answers (1)

dawid
dawid

Reputation: 207

When you define aop spring creates proxy around the class, so when the method is called, actually call is delegated to proxy, sth like

your.package.Engine$$FastClassBySpringCGLIB$$c82923b4.someMethodOfEngineClass()

But this works only when a method is called from outside it's class If you call class method from the same class you are effectively calling it by this.someMethodOfEngineClass()

here -> http://www.nurkiewicz.com/2011/10/spring-pitfalls-proxying.html you can find more info about proxying

so proxy is bypassed and aop is not working.

Upvotes: 4

Related Questions