Search code examples
springspring-bootspring-aop

Spring AOP for certain controller class


I have controllers in the same package

aa.bb.controller.A
aa.bb.controller.B
aa.bb.controller.C

I want to intercept all the calls to A and B, but not C. Because I don't want to use @Pointcut("within(aa.bb.controller..*), which apply to all three controllers.

2nd question is how to distinguish the request is for A or B.

Updated:

Thanks all for helping! I think it is pretty hard to implement what I originally thought. So I chose to do it separately for different controller. It gets the job done anyway. Hopefully there will be some solution in the future for what I wanted.


Solution

  • Please use the „execution“ instead of „within“ expression. This will help you to react on specific methode calls.

    @Pointcut("execution(* aa.bb.controller.A.*(..))")

    @Pointcut("execution(* aa.bb.controller.B.*(..))")

    Here, the first wildcard matches any return value, the second matches any method name, and the (..) pattern matches any number of parameters (zero or more).

    If you want to combine them please check following:

    @Pointcut("execution(* aa.bb.controller.A.*(..))")
    public void aMethods() {}
    
    @Pointcut("execution(* aa.bb.controller.B.*(..))")
    public void bMethods() {}
    
    @Pointcut("aMethods() || bMethods()")
    public void bothMethods() {}