javaspring-mvcaspectjpointcuts

Trying to match an AspectJ pointcut signature for any methods containing a variable


I want to create a pointcut that matches any method in my Web controller that contains a ModelMap:

pointcut addMenu(ModelMap modelMap) : 
    execution (public String example.web.MyController.*(..)) && args (modelMap);

before(ModelMap modelMap) : addMenu(modelMap) {
    // Do stuff with modelMap...
}

My problem is that this only matches methods with ONLY the ModelMap parameter, others are not matched because they contain too many parameters. For example, this is not intercepted, due to the "req" parameter:

public String request(HttpServletRequest req, ModelMap modelMap) {
    // Handle request
}

Is there any way to match all methods with a ModelMap parameter, without having to add a pointcut delegate for every possible parameter combination?


Solution

  • You can use wildcards * or .. to express the arguments in a flexible way.

    pointcut addMenu(ModelMap modelMap) : 
        execution (public String example.web.MyController.*(..)) && args (*, modelMap);
    

    See AspectJ: parameter in a pointcut