AOP not working with internal method call

31 views Asked by At

may be this is a duplicate question I am not sure. so please ping me if its exactly duplicate. But as far as I have searched this is little different than what is mentioned in the similar questions. So posting. Any help is appreciated.

@Aspect
@Component
public class MyAspect {
  @Pointcut("@annotation(com.home.CustomAnnotation)")
  public void myPointCut() {}

  @Around(value = "myPointCut")
  public void aroundAnnotation(ProceedingJoinPoint pjp) {
    syso("aspect working");
  }
}
public interface MyInterface {
  default void doProcess(){
    myProcess();
  }

  public void myProcess();
}
@Component
public class MyImplClass implements MyInterface {
  @CustomAnnotation
  public void myProcess(){
    syso("myProcess of MyImplClass");
  }
}
public class MyBusinessService {
  @Autowired
  private MyInterface myInterfaceRef;

  public void myService() {
    myInterfaceRef.doProcess();
  }
}

I also have these dependencies

implementation 'org.springframework.boot:spring-boot-starter-aop:2.7.15'
implementation 'org.aspectj:aspectjrt:1.9.7'
implementation 'org.aspectj:aspectjweaver:1.9.6'

and I am expecting that every time myProcess() method is invoked - then the advice "aspect working" would be printed. But it is not happening so. I understand that it is because AOP is not able to handle normal method invocation which is happening inside doProcess().

But how easily can I make this work? I tried this.myProcess() instead of myProcess() inside doProcess() but it does not work - when compiled --> "this." is omitted.

As the MyInterface is an interface --> I am not able to use self reference autowired. Kindly help if you know some cool way to deal with this.

0

There are 0 answers