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
.
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