Search code examples
javaspringaopaspectj

Aspectj Pointcut expression not working for one method and working for another method in single class


I have written below code in controller.

For this controllerMethod method the aspectj Pointcut expression is working fine but for executeService method Aspect is not working.

@RequestMapping(value = "URL", method = RequestMethod.POST)
public ModelAndView controllerMethod(@ModelAttribute ModelAttribute reqModel, HttpServletRequest req, HttpServletResponse res) {

    try {
        response = executeService(param1, param2);

    } catch (Exception e) {

    }

    }       
private ResponseObject executeService(String param1, String param2){
    //Code....
}

I have written the aspect as below.

@Before("execution(* com.*.*.Controller.executeService(..))")
public void logBefore(JoinPoint joinPoint) {
    logger.info("Before aspect: " + joinPoint.getSignature().getName());
}

Can you please let me know where is the issues. I need to execute aspect before calling executeService method.


Solution

  • Because the AOP not intercept internal call ,so you can add a self-controller field,and call the internal method by self.method(...). Following code:

    @Controller
    public class ExampleController{
    
        @Autowired
        private ExampleController self;
    
        @RequestMapping(value = "URL", method = RequestMethod.POST)
        public ModelAndView controllerMethod(@ModelAttribute ModelAttribute reqModel, HttpServletRequest req, HttpServletResponse res) {
    
            try {
                response = self.executeService(param1, param2);
    
            } catch (Exception e) {
    
            }
    
        }
        public ResponseObject executeService(String param1, String param2){
            //Code....
        }
    }