Search code examples
javaspringspring-mvcspring-aopaspect

Aspect method interception over a Controller


I have a Spring mvc controller with to methods:

@RequestMapping(value = "/method1", method = GET) 
public A method1() throws Exception 
{           
    return new A();
}

and

@RequestMapping(value = "/method2", method = GET) 
public int method2() throws Exception 
{           
    return -1;
}

I want to intercept these methods with an Aspect:

@Before("execution(** com.test.controller.*(..))")
public void startLog()
{
    System.out.println("START");
}

This aspect works ok with method1 and fails with method2. What I am doing wrong?


Solution

  • pointcut expression for methods in a particular package having @RequestMapping annotation:

    @Before("execution(* com.test.controller.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
        public void startLog()
        {
            System.out.println("START");
        }