Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify single pointcut for multiple packages

Tags:

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

  • com.package1 contains controller 1 class, let's name it as AController
  • com.package2 contains controller 2 class, let's name it as 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...(..))**.

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.

like image 786
Ketan Avatar asked Dec 08 '11 04:12

Ketan


People also ask

How do you write a pointcut expression?

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")

What is the difference between JoinPoint and pointcut?

A Joinpoint is a point in the control flow of a program where the control flow can arrive via two different paths(IMO : that's why call joint). A Pointcut is a matching Pattern of Joinpoint i.e. set of join points.

How do you define a point cut?

A pointcut is a program element that picks out join points and exposes data from the execution context of those join points. Pointcuts are used primarily by advice. They can be composed with boolean operators to build up other pointcuts.


2 Answers

You can use boolean operators:

expression="execution(* package1.*.*(..)) || execution(* package2.*.*(..))" 
like image 171
Sean Patrick Floyd Avatar answered Oct 12 '22 19:10

Sean Patrick Floyd


In case you use Annotations

@Pointcut("within(com.package1..*) || within(com.package2..*)") 
like image 36
Rohan Kushwaha Avatar answered Oct 12 '22 19:10

Rohan Kushwaha