Writing pointcuts

We can write pointcuts by using execution designator as follows:

  • execution(<method pattern>): The method must match the pattern as defined follows
  • Can chain together to create composite pointcuts by using following operators: && (and), || (or), ! (not)
  • Method pattern: Following is method pattern:
    • [Modifiers] ReturnType [ClassType]
    • MethodName ([Arguments]) [throws ExceptionType]

In the preceding method pattern, values within bracket [ ] that is, modifiers, ClassType, arguments and exceptions are all optional values. There is no need to define it for every pointcut using execution designator. Value without brackets such as ReturnType, and MethodName are mandatory to define.

Let's define a TransferService interface:

    package com.packt.patterninspring.chapter6.bankapp.service; 
    public interface TransferService { 
      void transfer(String accountA, String accountB, Long amount); 
    } 

TransferService is a service for transferring amounts from one to another account. Let's say that you want to write a logging aspect that triggers off TransferService's transfer() method. The following figure illustrates a pointcut expression that can be used to apply advice whenever the transfer() method is executed:

As in the preceding figure, you can see, I used the execution() designator to select join point TransferService's transfer() method. In preceding expression in figure, I have used an asterisk at the beginning of the expression. This means that method can return any type. And after asterisk, I have specified a fully qualified class name and name of method as transfer(). As method arguments, I have used double dot (..), it means that the pointcut can select a method whose name is transfer() with no parameter or any number of parameters.

Let's see following some more pointcut expressions to select join points:

  • Any class or package:
    • execution(void transfer*(String)): Any method starting with transfer that takes a single String parameter and has a void return type
    • execution(* transfer(*)): Any method named transfer() that takes a single parameter
    • execution(* transfer(int, ..)): Any method named transfer whose first parameter is an int (the ".." signifies zero or more parameters may follow)
  • Restrict by class:
    • execution(void com.packt.patterninspring.chapter6.bankapp.service.TransferServiceImpl.*(..)): Any void method in the TransferServiceImpl class, it is including any sub-class, but will be ignored if a different implementation is used.
  • Restrict by interface:
    • execution(void com.packt.patterninspring.chapter6.bankapp.service.TransferService.transfer(*)): Any void transfer() method taking one argument, in any object implementing TransferService, it is more flexible choice--works if implementation changes.
  • Using Annotations
    • execution(@javax.annotation.security.RolesAllowed void transfer*(..)): Any void method whose name starts with transfer that is annotated with the @RolesAllowed annotation.
  • Working with packages
    • execution(* com..bankapp.*.*(..)): There is one directory between com and bankapp
    • execution(* com.*.bankapp.*.*(..)): There may be several directories between bankapp and com
    • execution(* *..bankapp.*.*(..)): Any sub-package called bankapp

Now that you have seen that the basics of writing pointcuts, let's see how to write the advice and declare the aspects that use those pointcuts

..................Content has been hidden....................

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