Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring pointcut designators differences (within vs execution)

Please... can anybody explain me what are the differences between using the following spring pointcut designators?

Using "within pointcut designator":

<aop:pointcut expression="within(my.app.dao.impl.*)" id="commonDaoOperation"/>

Using "execution pointcut designator":

<aop:pointcut expression="execution(public * my.app.dao.impl.*.*(..))" id="commonDaoOperation"/>

I am using the second one in my web-projects (and I think it's the most used), the problem i have found with this approach is that it's consuming a lot of memory in the heap...

After analyzing the "heap dump" of the application server with the "eclipse memory analyzer" i have found that my application is consuming 450 MB and the instances of the class "org.springframework.aop.aspectj.AspectJExpressionPointcut" are consuming 30% of those 450MB...

Each instance of AspectJExpressionPointcut occupy 6 MB (approximately) and this is because each instance mantains a cache of matches with instances of java.lang.reflect.Method and surprisingly there are a lot of java methods cached (methods that my pointcut expression doesnt mentions).

After Reading Spring Documentation, I decided to use the first one approach (within pointcut designator) and now each instance of AspectJExpressionPointcut occupy much less memory.

The question is about that... what is the difference in performance between them...

Many thanks in advance...

like image 629
glazaror Avatar asked Dec 28 '13 16:12

glazaror


People also ask

Is the following pointcut expression correct execution?

Is the following pointcut expression correct? Explanation: You can omit the package name if the target class or interface is located in the same package as this aspect. Explanation: Annotations must be added to the implementation class but not the interface, as they will not be inherited.

Which of the following AspectJ pointcut designators are supported in Spring AOP?

Spring AOP supports the following Pointcut Designators (PCD). execution – for matching method execution join points. This is the most widely used PCD. within – for matching methods of classes within certain types e.g. classes within a package.

What method does this pointcut expression reference within?

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 having the fist parameter of type Long, we could use the following expression: @Pointcut("execution(* *.. find*(Long,..))")

What is the difference between execution and within in Spring AOP?

The Spring documentation explains the difference: In other words, execution matches a method and within matches a type. In this case, your pointcuts are pretty much equivalent. Your within matches any type in the package my.


2 Answers

execution() matches join points that are method executions. This is the only designator that actually performs matches. All other designators (supported by Spring AOP) only limit those matches. Note that Spring supports only a subset of designators available in AspectJ (Spring AOP is proxy-based). AspectJ pointcut designators that are supported in Spring are: args() and @args(), target() and @target(), within() and @within(), execution(), this(), @annotation

like image 163
Sabina Orazem Avatar answered Oct 03 '22 10:10

Sabina Orazem


The Spring documentation explains the difference:

  • execution - for matching method execution join points, this is the primary pointcut designator you will use when working with Spring AOP
  • within - limits matching to join points within certain types (simply the execution of a method declared within a matching type when using Spring AOP)

In other words, execution matches a method and within matches a type.

In this case, your pointcuts are pretty much equivalent. Your within matches any type in the package my.app.dao.impl and your execution matches all the public methods of any type in the package my.app.dao.impl.

However, execution is implemented, I think, with an interceptor for each matched method (a lot of objects), which within only needs one interceptor since it matches the entire type (very little objects).

like image 35
Sotirios Delimanolis Avatar answered Oct 03 '22 09:10

Sotirios Delimanolis