The intention of this blog is to explain AspectJ Pointcut Expressions in Spring Applications. In Aspect Oriented Programming, a pointcut is a set of joinpoints. A joinpoint is a point in program execution where you can add additional behavior. Spring applications only support method based joinpoints. Lets discuss some pointcut expressions patterns.
Method Signature Patterns
The most typical pointcut expressions are used to match a number of methods by their signatures. A common method based
pointcut expression is something like
expression(<method scope> <return type> <fully qualified class name>.*(parametes))
- method scope: Advice will be applied to all the methods having this scope. For e.g., public, private, etc. Please note that Spring AOP only supports advising public methods.
- return type: Advice will be applied to all the methods having this return type.
- fully qualified class name: Advice will be applied to all the methods of this type. If the class and advice are in the same package then package name is not required
- parameters: You can also filter the method names based on the types. Two dots(..) means any number and type of parameters.
- execution(* com.aspects.pointcut.DemoClass.*(..)) : This advice will be applied to all the methods of DemoClass.
- execution(* DemoClass.*(..)): You can omit the package if the DemoClass and the advice is in the same package.
- execution(public * DemoClass.*(..)): This advice will be applied to the public methods of DemoClass.
- execution(public int DemoClass.*(..)): This advice will be applied to the public methods of DemoClass and returning an int.
- execution(public int DemoClass.*(int, ..)): This advice will be applied to the public methods of DemoClass and returning an int and having first parameter as int.
- execution(public int DemoClass.*(int, int)): This advice will be applied to the public methods of DemoClass and returning an int and having both parameters as int.
Type Signature Patterns
These pointcut expressions are applied to all joinpoint of certain types. A common type signature patterns looks like
Here type name is either the package name or the class name.
- within(com.aspects.blog.package.*) : This will match all the methods in all classes of com.aspects.blog.package.
- within(com.aspects.blog.package..*) : This will match all the methods in all classes of com.aspects.blog.package and its sub packages. The only difference is the extra dot(.) after package.
- within(com.aspects.blog.package.DemoClass) : This will match all the methods in the DemoClass.
- within(DemoClass) : Again, if the target class is located in the same package as this aspect, the package name can be omitted.
- within(DemoInterface+) : This will match all the methods which are in classes which implement DemoInterface.
Bean Name Patterns
Spring 2.5 supports a new pointcut type that is used to match bean names. For example, the following pointcut
expression matches beans whose name ends with Service.
This pointcut is not supported by AspectJ annotation, hence you can declare them only in spring context files.
Combining Pointcut Expressions
Pointcut expressions can be combined using && (and), ||(or), and !(not). For example,
within(DemoInterface1+) || within(DemoInterface2+)
The above patterns will match all join point in all classes which implement DemoInterface1 or DemoInterface2