Spring AOP :Pointcut in details


Pointcuts determine join points of interest, and thus enable us to control when advice executes. Spring AOP only supports method execution join points for Spring beans.
A pointcut declaration has two parts: a signature comprising a name and any parameters, and a pointcut expression that determines exactly which method executions we are interested in.

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)
this – limits matching to join points (the execution of methods when using Spring AOP) where the bean reference (Spring AOP proxy) is an instance of the given type
target – limits matching to join points (the execution of methods when using Spring AOP) where the target object (application object being proxied) is an instance of the given type
args – limits matching to join points (the execution of methods when using Spring AOP) where the arguments are instances of the given types

Pointcut expressions can also be combined using ‘&&’, ‘||’ and ‘!’. It is also possible to refer to pointcut expressions by name.

The following example shows three pointcut expressions:
anyPublicOperation (which matches if a method execution join point represents the execution of any public method); inTrading and tradingOperation :

execution(public * *(..))
private void anyPublicOperation() {}

within(com.xyz.someapp.trading..*
private void inTrading() {}

anyPublicOperation() && inTrading()
private void tradingOperation() {}

Following are the examples showing different manner in which a pointcut can be declared:

the execution of any public method:
execution(public * *(..))

the execution of any method with a name beginning with “set”:
execution(* set*(..))

the execution of any method defined by the MyService interface
execution(* com.xyz.service.MyService.*(..))

the execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..))

the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service..*.*(..))

any join point (method execution only in Spring AOP) within the service package:
within(com.xyz.service.*)

any join point (method execution only in Spring AOP) within the service package or a sub-package:
within(com.xyz.service..*)

any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:
this(com.xyz.service.AccountService)

any join point (method execution only in Spring AOP) where the target object implements the AccountService interface:
target(com.xyz.service.AccountService)

any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable:
args(java.io.Serializable)
To know how to use pontCut in sprion AOP follow up previous post Spring AOP

The content is referred from Spring Doc

9 thoughts on “Spring AOP :Pointcut in details

    1. I agree with Rickard and James, and even Hani! AOP would be a great way to specify dlteuafs that makes sense to you in you application.I think Rickards indirection is actually quite useful. His pointcut is based on the setter’ annotation and then there is a pointcut that introduces the setter’ annotation based on well-known naming schemes, but whenever he choose to divert from those naming schemes he can just add the setter’ and it would just work!(The next version of AJDT will support renaming classes or methods and it would get updated in a fully qualified pointcut.)

  1. can someone please post a complete example of :
    args(java.io.Serializable). how to use this and what would be the difference if i use execution and use java.io.Serializabe instead of .. to pass the number of arguments.

    1. I agree with James’ last comments. On the one hand pepole are saying as long as there are good defaults it’ll be fine , and on the other using AOP to apply annotations is evil . What most don’t seem to realize is that AOP is a good way to apply those defaults. For example, if all *Impl classes are sessions, and all *Bean classes are entities, why not use an AOP pointcut to introduce those annotations as defaults? Aspects, or services, using the annotation does not have to care *how* that annotation got there, but only that it *is* there.Also, about the breaking through annotation part. Most of those issues are solved by using a decent AOP browser to verify that everything is applied as intended.

  2. ку
    пару дней назад юзер Admin сказал разместить интересную ссылку на сайт с классной одеждой для мужиков

    http://letvqc.sed4admin.ru/img/ms.png

    с любовью

    а еще нас видят по ключевым словам:
    приобрести кофта для мужчин новые модели россия 2015
    скидки пуловер связанный мужской новые модели киев 2013
    скидки кардиган вязаный для мужчин
    джемпер мужской москва 2013
    купить свитер связанный для мужчин фото украина 2015
    купить пуловер вязаный для мужчин модные 2013
    стоимость свитер вязаный для мужчин модные спб 2014
    заказать джемпер вязаный мужской фото санкт петербург 2013
    заказ свитер вязаный мужские новинки санкт петербург
    заказ кардиган ажурный для мужчин новые модели москва 2013
    скидки пуловер связанный мужской модные
    скидки джемпер мужской новые модели киев 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s