Photo by Glenn Carstens Peters |
What is Spring AOP?
Spring AOP enables Aspect-Oriented Programming in spring applications. It provides the way to dynamically add the cross-cutting concerns(logging, security, transaction management, auditing, i18n etc.) before, after or around the actual logic using simple pluggable configurations. It makes easy to maintain code in the present and future as well. You can add/remove concerns without recompiling complete source code simply by changing configuration files (if you are applying aspects using XML configuration).
What is advice, joinpoint or pointcut?
- An important term in AOP is advice. It is the action taken by an aspect at a particular join-point.
- Joinpoint is a point of execution of the program, such as the execution of a method or the handling of an exception. In Spring AOP, a joinpoint always represents a method execution.
- Pointcut is a predicate or expression that matches join points.
- Advice is associated with a pointcut expression and runs at any join point matched by the pointcut.
- Spring uses the AspectJ pointcut expression language by default.
Pointcut
Pointcut determines the join point of interest and in the code it appears as pointcut expression. It works similarly to regular expressions, using the special syntax it matches methods with advices. Please note, that Spring AOP supports only those classes that are defined as Spring beans/component otherwise they won’t be available. Here is a pointcut expression general syntax (those parts that are in red are mandatory) with some examples:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)
The following examples show some common pointcut expressions:
The execution of any public method, First * means that it will match any return type, and *(..) means that the expression will match any method, no matter how much arguments it contains.
execution(public * *(..))
The execution of any method with a name that begins with set:
execution(* set*(..))
The execution of any method defined by the OrderService interface/class:
execution(* com.bhargav.service.AccountService.*(..))
The execution of any method defined in the service package or one of its sub-packages:
execution(* com.bhargav.service..*.*(..))
This will be matched only for those methods in the DemoClass, which has int as a first parameter, return type as int and method should be public:
execution(public int DemoClass.*(int, ..))
Any join point (method execution only in Spring AOP) within the service package:
within(com.bhargav.service.*)
Advices
Spring AOP includes the following types of advice:
@Before:
Advice that runs before a join point but that does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
@After:
Advice to be run regardless of the means by which a join point exits (normal or exceptional return).
@AfterReturning:
Advice to be run after a join point completes normally (for example, if a method returns without throwing an exception).
@AfterThrowing:
Advice to be run if a method exits by throwing an exception.
@Around:
Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behaviour before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
References:
- AspectJ