Search code examples
javaspringaspectjspring-aoppointcut

pointcut for method in parent abstract class


I have a scenario where my method to be intercepted is in the parent class and is not overridden in the pointcut class. Here is the sample classes:

public abstract class A{
@RequestMapping(value = "/data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getData(@RequestBody String request) throws Exception {
    return "dummy";
}
}

@RestController
public class B extends A {
}

My Aspect is defined as:

@Aspect
@Component
public class RestCallLogger {
    @Pointcut("within(com.test..*) && within(@org.springframework.web.bind.annotation.RestController *)")
    public void restControllers() {
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void requestMappingAnnotations() {
    }

    @Around("restControllers() && requestMappingAnnotations()")
    public Object onExecute(ProceedingJoinPoint jp) throws Throwable {
        Object result = null;
        try {
            result = jp.proceed();
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }
}

But its not working. If I mark class A with Annotation @RestController and make it concrete, then it works. The question is how can I create a "pointcut for method in parent abstract class"? PS: I can not change the hierarchy of the code as its the existing code.


Solution

  • For me this works. Here is an MCVE:

    package de.scrum_master.app;
    
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    public abstract class A {
      @RequestMapping(value = "/data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
      public String getData(@RequestBody String request) throws Exception {
        return request;
      }
    }
    
    package de.scrum_master.app;
    
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class B extends A {}
    
    package de.scrum_master.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @ComponentScan(basePackages = { "de.scrum_master" })
    public class Application2 {
      public static void main(String[] args) throws Exception {
        ApplicationContext appContext = new AnnotationConfigApplicationContext(Application2.class);
        B b = (B) appContext.getBean("b");
        System.out.println(b.getData("bbb"));
        A a = (A) appContext.getBean("b");
        System.out.println(a.getData("aaa"));
      }
    }
    
    package de.scrum_master.aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class RestCallLogger {
      @Pointcut("within(de.scrum_master..*) && @target(org.springframework.web.bind.annotation.RestController)")
      public void restControllers() {}
    
      @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
      public void requestMappingAnnotations() {
      }
    
      @Around("restControllers() && requestMappingAnnotations()")
      public Object onExecute(ProceedingJoinPoint jp) throws Throwable {
        System.out.println(jp);
        Object result = null;
        try {
          result = jp.proceed();
        } catch (Exception ex) {
          throw ex;
        }
        return result;
      }
    }
    

    The console log says:

    execution(String de.scrum_master.app.A.getData(String))
    bbb
    execution(String de.scrum_master.app.A.getData(String))
    aaa
    

    What is different in your case?