Reputation: 3282
I am using Aspect for logging activities in my spring mvc based application. I am using @controller annotations to define any controller in my application. I have two different controller in two different package say
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...(..))**.
Right now I am declaring one separate pointcut for each package and in aspect one separate aop:before
and aop:after
entry for each pointcut. But I think this should be ideal way to define multiple packages pointcut.
Upvotes: 24
Views: 41104
Reputation: 553
@Pointcut("execution(* pakageName.anyClassNameX.*(..)) || execution(* pakageName.anyClassNameY.*(..))")
public void allMethods() {
}
@Around("allMethods()")
public Object intercept(ProceedingJoinPoint thisJoinPoint) throws Throwable {
try {
return thisJoinPoint.proceed();
} catch (Exception ex) {
}
}
Upvotes: 2
Reputation: 1062
In spring Boot
@Before("execution(* PackageName.Controller.Service.MethodName(..))
||execution(* PackageName.Controller.Service.*.*(..))")
Example Spring-projects/AOP
Upvotes: 4
Reputation: 745
In case you use Annotations
@Pointcut("within(com.package1..*) || within(com.package2..*)")
Upvotes: 20
Reputation: 298908
You can use boolean operators:
expression="execution(* package1.*.*(..)) || execution(* package2.*.*(..))"
Upvotes: 62