Search code examples
springspring-mvcannotationsaspectjspring-annotations

Spring @Around Aspect being ignored in one controller but not in other one


I have a weird issue - my aspect is ignored in one specific controller, but works in others...

context.xml includes:

<mvc:annotation-driven />
<task:annotation-driven />
<aop:aspectj-autoproxy />
<context:spring-configured />
<context:component-scan base-package="com.my.package.address.aspect"/>
<context:component-scan base-package="com.my.package.address.controllers" />

aspects declaration:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DoWork {
}

the aspect itself:

@Aspect
@Configurable
public class DoWorkAspect {

    @Around("@annotation(com.my.package.address.aspect.DoWork)")
  public Object doWork(ProceedingJoinPoint joinPoint) throws Throwable {
      ...
  }
}

the controller that the aspect fires on (works):

@Controller
@RequestMapping(value = "/ping")
@Validated
public class PingController {

    @DoWork
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public PingResponse ping(@PathVariable("id") String id) throws Exception {
        return new PingResponse("ok");
    }

and, the controller that the aspect ignores:

@Controller
@RequestMapping(value = "/not_ping")
public class NotPingController
{
    @DoWork
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseStatus(value = HttpStatus.OK)
    protected void notPing(
         @PathVariable("id") String id
         ) throws
            IOException,
            NotAuthorizedException
    {
        ...
    }
}

So one controller gets ignored while the other works... Any ideas why?


Solution

  • I see your method NotPingController.notPing() is a protected method.

    According to spring's documentation "Due to the proxy-based nature of Spring's AOP framework, protected methods are by definition not intercepted "

    Can you try making that method public and check whether it is been intercepted or not