1

I know that there are a lot of similar questions in Stackoverflow but none of them helped me.

I have a controller like this:

com.mypkg.controller;

@RestController
public class MyController {

  @RequestMapping(method = RequestMethod.POST,
   ......
  public ResponseEntity<?> MyEndpoint(myParams) {
      return this.myMethod(myParams, "myString");
  }

  public ResponseEntity<?> myMethod(myParams, String myString){
     //do something
     return myReponseEntity
  }
}

I defined my aspect in this way:

com.mypkg.controller;

@Aspect
@Component
@Slf4j
public class MyAspect {
    @Around("execution(* com.mypkg.controller.MyController.MyEndpoint(..))  && args(..,aParam)")
    public ResponseEntity<?> endpointAround(ProceedingJoinPoint joinPoint, String aParam) throws Throwable {
        // I am working fine
        // do something
        return 
   }

    @Around("execution(* com.mypkg.controller.MyController.myMethod(..))  && args(..,myString)")
    public ResponseEntity<?> myMethodAround(ProceedingJoinPoint joinPoint, String myString) throws Throwable {
        // **** I AM NOT CALLED****
        // do something
        // return ...
   }
}

I configured the AutoProxy

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AopConfig {}

The function endpointAround is called every time that I call MyEndpoint (throw the REST api).

The problem is the second @Around. it is not called. I need to call a method everytime MyEndpoint is exectued and another one eveytime that MyEndpoint call myMethod.

Fabry
  • 1,498
  • 4
  • 23
  • 47
  • Possible duplicate of [AfterReturning annotation not working for specific method structure](https://stackoverflow.com/questions/57427286/afterreturning-annotation-not-working-for-specific-method-structure) – kriegaex Sep 06 '19 at 11:57
  • See also [here](https://stackoverflow.com/a/52126293/1082681), [here](https://stackoverflow.com/a/48808870/1082681), [here](https://stackoverflow.com/a/40566024/1082681), all the same issue. I have answered that so many times and it is so well-documented... No offense meant. – kriegaex Sep 06 '19 at 11:59

2 Answers2

0

The problem is that your method myMethod is call from within your other method directly, and not as a someSpringBean.myMethod.

The way spring works is by wrapping any of your beans, and then on the 'wrapping' it can execute all the aspect or other spring related stuff. When you call one method from another one inside the same class, you don't go through the wrapping, thus the aspect related stuff can't happen

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
0

You just missed some code. Let us use below code snippet it will work. Use this com.mypkg.controller.MyController.myMethod instead of com.mypkg.controller.myMethod it will work

Controller

com.mypkg.controller;

@RestController
public class MyController {

  @RequestMapping(method = RequestMethod.POST,
   ......
  public ResponseEntity<?> MyEndpoint(myParams) {
      return this.myMethod(myParams, "myString");
  }

  public ResponseEntity<?> myMethod(myParams, String myString){
     //do something
     return myReponseEntity
  }
}

and in Aspect

com.mypkg.controller;

@Aspect
@Component
@Slf4j
public class MyAspect {
    @Around("execution(* com.mypkg.controller.MyController.MyEndpoint(..))  && args(..,aParam)")
    public ResponseEntity<?> endpointAround(ProceedingJoinPoint joinPoint, String aParam) throws Throwable {
        // I am working fine
        // do something
        return 
   }

    @Around("execution(* com.mypkg.controller.MyController.myMethod(..))  && args(..,myString)")
    public ResponseEntity<?> myMethodAround(ProceedingJoinPoint joinPoint, String myString) throws Throwable {
        // **** I AM NOT CALLED****
        // do something
        // return ...
   }
}

You just missed the package path. Your method path should be like this...springbean.method

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
  • thank you I didn't miss the package path, I just forgot to add it here. Sorry for that. As I said `endpointAround` is called and has the same execution definition – Fabry Sep 06 '19 at 07:24
  • @Fabry How your myMethod getting called. I didn't see any mapping because for me above code snippet is getting executed properly. – Maheshwar Ligade Sep 06 '19 at 07:48