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

Advertisements