Defining pointcuts

As we learned before, pointcuts define a point where advice should be applied. Spring AOP uses AspectJ's expression language to define a point where advice should be applied. The following are the set of pointcut designators supported in Spring AOP:

Designator

Description
execution It restricts matching to join points by a method execution. 
within

It restricts matching to join points within certain types only.

Example: within(com.packt.springhighperformance.ch3.TransferService).

args

It restricts matching to join points where arguments are of the given type.

Example: args(account,..).

this

It restricts matching to join points where the bean reference or Spring proxy object is an instance of the given type.

Example: this(com.packt.springhighperformance.ch3.TransferService).

target

It restricts matching to join points where the target object is an instance of the given type.

Example: target(com.packt.springhighperformance.ch3.TransferService).

@within

It restrict matching to join points where the declared type has the given type of annotation.

Example: @within(org.springframework.transaction.annotation.Transactional).

@target

It restricts matching to join points where the target object has the given type of annotation.

Example: @target(org.springframework.transaction.annotation.Transactional).

@args

It restricts matching to join points where the type of the actual arguments passed have annotations of the given type.

Example: @args(com.packt.springhighperformance.ch3.Lockable).

@annotation

It restricts matching to join points where the executing method has the given annotation.

Example: @annotation(org.springframework.transaction.annotation.Transactional).

 

Let's see how to write the point expression using the execution designator:

  • Using execution(<method-pattern>): Method matching to the pattern would be advised. The following is the method pattern:
[Modifiers] ReturnType [ClassType]
MethodName ([Arguments]) [throws ExceptionType]
  • To create composite pointcuts by joining other pointcuts, we can use the &&, ||, and ! operators (these mean AND, OR, and NOT, respectively).

In the preceding method pattern, anything defined in [ ] is optional. Values without [ ] are mandatory to define.

The following diagram will illustrate point expression using the execution designator to apply advice whenever the findAccountById() method is executed:

Execution join point pattern
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset