Spring pointcut designators differences (within vs execution)
Asked Answered
U

3

16

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...

Uncle answered 28/12, 2013 at 16:32 Comment(0)
S
9

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).

Stoecker answered 28/12, 2013 at 16:38 Comment(9)
Thanks Sotirios Delimanolis, Can i use them together without any problems? Recently I have changed my configuration to use them together (within and execution)... something like the following: '<'aop:pointcut expression="within(my.app.dao.impl.*) AND execution(public * my.app.dao.impl.*.*(..))" And the memory consumption of instances of class "AspectJExpressionPointcut" was reduced very much! So Is this recommended to use them together?Uncle
@Uncle There's no real point using them together. The within already matches everything in your execution.Stoecker
Ok Sotirios Delimanolis, you have the reason... I should only use the "within".Uncle
In my "use case" the instances of "AspectJExpressionPointcut" were the problem in memory consumption because of "execution pointcut designator"... changing that for "within pointcut designator" reduces that problem.Uncle
So... can it be considered better to use within pointcut designator in some use cases?Uncle
@Uncle It's not better. Each has its own purpose. For what you are trying to achieve, within is more suitable.Stoecker
glazaror: Regarding your first comment, I would say @SotiriosDelimanolis threw you off the right track there. As per the Spring AOP documentation: The existing designators naturally fall into one of three groups: kinded, scoping and context: 1) Kinded designators are those which select a particular kind of join point. For example: execution, get, set; 2) Scoping designators are those which select a group of join points of interest (of probably many kinds). For example: within, withincode;Krasnodar
glazaror: The Spring AOP documentation then goes on to say: A well written pointcut should try and include at least the first two types (kinded AND scoping), whilst the contextual designators may be included if wishing to match based on join point context, or bind that context for use in the advice. Supplying either JUST a kinded designator or JUST a contextual designator will work but could affect weaving performance (time and memory used) due to all the extra processing and analysis.Krasnodar
So to sum up: seems as if using BOTH the execution and within designators in your example isn't that redundant after all.Krasnodar
C
1

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

Culley answered 2/2, 2019 at 17:57 Comment(0)
S
0

As mentioned here:

  • execution() - match a joinPoint method’s signature
  • within() - match all the JoinPoint methods in a given class, package, or sub-package

Other existing types of pointcut expressions: args(), target(), this(), @args(), @within(), @target(), @annotation()

Schizophyceous answered 19/2, 2023 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.