Skip to main content

Pointcut

Declaring a Pointcut

  • Only supports join points for Spring beans
@Pointcut("execution(* transfer(..))") // pointcut expression
private void anyOldTransfer() {} // pointcut signature
Pointcut DesignatorsDescription
executionMatching execution join points (primary one used)
withinMethod within a matching type
thisMatching bean reference of given type
targetMatching target object type
argsWhere arguments are of given types
@targetwhere class of executing object has annotation of given type
@argswhere runtime type of arguments match
@annotationwhere subject of join point has given annotation
bean(idOrNameOfBean)Target specific bean
warning

Due to proxy-based nature, only public methods can be intercepted (protected methods cannot be accessed)

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

Examples

ExampleDescription
execution(public * *(..))The execution of any public method
execution(* set*(..))The execution of any method beginning with set
execution(* com.xyz.service.*.*(..))The execution of any method in the service package
within(com.xyz.service.*)any join point within service package
within(com.xyz.service..*)any joint point within service package or sub-package
this(com.xyz.service.AccountServiceany join point where proxy implements AccountService interface
target(com.xyz.service.AccountServiceany join point where target implements AccountService interface
args(java.io.Serializableany join point which takes a single parameter where runtime argument is Serializable
@target(org.springframework.transaction.annotation.Transactionalany join point where the target object has an @Transactional annotation
@within(org.springframework.transaction.annotation.Transactionalany join point where declared type of target object @Transactional annotation
@annotation(...annotation.Transactionalany join point where executing method has @Transactional annotation
@args(com.xyz.security.Classifiedany join point which takes a single parameter where the runtype has @Classified

Writing good pointcuts

  • Dynamic match - match cannot be fully formed from static analysis => test will be placed in code to determine if there is actual match
  • AspectJ will rewrite it into optimal form for matching process
  • Pointcuts ar e written in DNF and the components of the pointcut are sorted such that components that are cheaper to evaluate are evaluated first
  • Points to consider for optimal performance of matching
    • How to narrow search space
  • Designator Types (Pointcuts)
    • Kinded: select a particular kind of joint (e.g. execution, get, set, call, handler)
    • Scoping: Select a group of join points of interest (e.g. within, withincode)
    • Contextual: maatch and optionally bind based on context(@annotate)
note

A well written pointcut should try and include at least the first two types of scoping (kinded and scoping)

Contextual designators may be included if wished to match based on join point context or bind context for use in advice

Supplying either just a kinded designator or just a contextual designator will work but could affect weaving performance

Scoped designators are very fast to match (should include one if possible)