spring 如何为多个包指定单个切入点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8426308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:48:04  来源:igfitidea点击:

How to specify single pointcut for multiple packages

springaopspring-aoppointcutaspect

提问by Ketan

I am using Aspect for logging activities in my spring mvc based application. I am using @controllerannotations to define any controller in my application. I have two different controller in two different package say

我正在使用 Aspect 在基于 spring mvc 的应用程序中记录活动。我正在使用@controller注释来定义我的应用程序中的任何控制器。我在两个不同的包中有两个不同的控制器说

  • com.package1 contains controller 1 class, let's name it as AController
  • com.package2 contains controller 2 class, let's name it as BController
  • com.package1 包含控制器 1 类,我们将其命名为 AController
  • com.package2 包含控制器 2 类,我们将其命名为 BController

I am able to apply aspect to one particular package of controllers by using

我能够通过使用将方面应用于一个特定的控制器包

<aop:config>
    <aop:pointcut id="pointcut1"
        expression="execution(* package1.*.*(..))"
        id="policy1" />
    <aop:aspect ref="aspect1" order="1">
        <aop:before pointcut-ref="pointcut1" method="before" arg-names="joinPoint" />
        <aop:after-returning returning="returnValue" arg-names="joinPoint, returnValue" pointcut-ref="pointcut1" method="after"  />
    </aop:aspect>
</aop:config>


<bean id="aspect1" class="com......aspectclass" />

My question is how to specify more that one different package in expression(* package1...(..))**.

我的问题是如何在expression(* package1. ..(..)) ** 中指定更多不同的包。

Right now I am declaring one separate pointcut for each package and in aspect one separate aop:beforeand aop:afterentry for each pointcut. But I think this should be ideal way to define multiple packages pointcut.

现在,我正在为每个包声明一个单独的切入点,aop:beforeaop:after为每个切入点声明一个单独的入口。但我认为这应该是定义多个包切入点的理想方式。

回答by Sean Patrick Floyd

You can use boolean operators:

您可以使用布尔运算符:

expression="execution(* package1.*.*(..)) || execution(* package2.*.*(..))"

回答by Rohan Kushwaha

In case you use Annotations

如果您使用注释

@Pointcut("within(com.package1..*) || within(com.package2..*)")

回答by Dapper Dan

In spring Boot

在春季启动

@Before("execution(* PackageName.Controller.Service.MethodName(..))          
  ||execution(* PackageName.Controller.Service.*.*(..))")

Example Spring-projects/AOP

示例Spring 项目/AOP