@AspectJ pointcut for all methods inside package

前端 未结 3 1813
悲&欢浪女
悲&欢浪女 2021-02-01 15:45

I have this working code for a specific package, but i want to configure it for all controllers, service and dao packages Eg

相关标签:
3条回答
  • 2021-02-01 16:11

    You just need to change your point cut to something like this :

    @Pointcut("within(com.abc.*)")
    

    Further reading - https://docs.spring.io/spring/docs/2.0.x/reference/aop.html

    0 讨论(0)
  • 2021-02-01 16:18

    Another alternative is to use

    @Pointcut("bean(*Controller)")
    

    But naming of your beans should be corresponding

    0 讨论(0)
  • 2021-02-01 16:22

    How about one of these alternatives?

    A) General execution pointcut with package restrictions:

    execution(* *(..)) &&
    (
        within(com.abc.xyz..controller..*) ||
        within(com.abc.xyz..service..*) ||
        within(com.abc.xyz..dao..*)
    )
    

    B) Package-restricted execution pointcuts:

    execution(* com.abc.xyz..controller..*(..)) ||
    execution(* com.abc.xyz..service..*(..)) ||
    execution(* com.abc.xyz..dao..*(..))
    

    I prefer B, by the way, just because it is a bit shorter and easier to read. As you have probably guessed, the .. notation means "any package or subpackage", whereas * at the end of the expression after .. means "any method in any class".

    0 讨论(0)
提交回复
热议问题