Question

I have a standalone aspectJ library ( not related to spring in any way) that I added to app server classpath to monitor the running applications using Load Time Weaving

The aspect work perfectly with all methods ( domain, service, DAO...) but for some reason it doesn't recognize Spring MVC methods with @RequestMapping annotation even if the pointcut expression is matching the controller class and the method name

Is there a different rule for Spring MVC controllers mapping methods ( or Controllers in general from other frameworks too) that needs to be handled separately.

The aspect weaves into other public and private methods in the @Controller classes but not into the @RequestMapping methods specifically.

Note: i m not looking for a spring MVC specific solution ( unless it is the only option), I want to be able to weave into any type of classes and methods regardless of the framework without having to modify the running applications.

Here is my code.

public aspect MethodExecutionTimeAspect {

private CollectedData collectedData;

public MethodExecutionTimeAspect() {
    // some initialiation logic.
}


pointcut publicOperation() : execution(public * com.mycompany.myproject..*());

Object around() : publicOperation() {
    // do something
    Object ret = proceed();
     // do something.
    return ret;
  }

}

package com.mycompany.myproject.webapp.springmvc;


@Controller
public class HomeController {

@Resource(name="productService")
private ProductService productService;

@RequestMapping(value = {"/home.do", "/"})
public String displayHomePage(ModelMap map) {
    List<Product> featuredProducts = productService.getFeaturedProducts();
    map.put("featuredProducts", featuredProducts);
    return "homePage";
}

}

Was it helpful?

Solution

The pointcut only matches methods without any arguments. The displayHomePage method takes a map parameter. Try

pointcut publicOperation() : execution(public * com.mycompany.myproject..*(..));

With the added .. this pointcut will match any public methods within your specified package, regardless of how many arguments there are - if any.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top