Spring AOP
June 21, 2020

M02 Q02 What is a pointcut, a join point, an advice, an aspect, weaving?

What is Join Point?

Firstly, let's consider the Join Point because in AOP everything is executed around a Join Point.

Join Point in aspect-oriented programming is a point in the execution of a program in which behaviour can be altered by AOP.

In Spring AOP Join Point is always method execution. So whenever one component is executing the method on another component is a Join Point.

Each execution of the method from the external part of the component is considered as join point. Keep in mind that join point cannot be an execution of the method within the class. Self-invocation is not supported by AOP. Only execution from outside of the component is supported.

Aspect-Oriented Programming concept, in general, distinguishes additional Join Points, some of them include:

  • Method Execution / Invocation
  • Constructor Execution / Invocation
  • Reference / Assignment to Field  Exception Handler
  • Execution of Advice
  • Execution of Static Initializer / Object Initializer

What is Pointcut?

The Pointcut is a predicate used to match join point. Additional code, called Advice is executed in all parts of the program that are matching pointcut. Spring uses the AspectJ pointcut expression language by default.

It is a way to inform AOP in which part of the code we want to modify the behaviour.

Example of Pointcut Expressions:

  • execution - Match Method Execution
  • within - Match Execution of given type or types inside package
  • @within – Match Execution of type annotated with annotation
  • @annotation – Match join points where the subject of the join point has the given annotation
  • bean – Match by spring bean name
  • args – Match by method arguments
  • @args – Match by runtime type of the method arguments that have annotations of the given type
  • this – Match by bean reference being an instance of the given type (for CGLIB-based proxy)
  • target – Match by target object is an instance of the given type
  • @target – Match by a class of the executing object has an annotation of the given type

What is Advice?

Advice is additional behaviour that will be inserted into the code, at each join point matched by pointcut.

Advice examples

What is Aspect?

Aspect brings together Pointcut and Advice. Usually, it represents single behaviour implemented by advice that will be added to all join points matched by pointcut.

Remember, that Aspect is Spring needs to be a bean.

What is Weaving?

Weaving is the process of applying aspects, which modifies code behaviour at join points that have matching pointcuts and associated advices. During weaving aspects and application code is combined which enables execution of cross-cutting concerns.

Types of weaving:

Compile-Time Weaving – byte code is modified during the compilation, aspects are applied, code is modified at join points matching pointcuts by applying advices. For example Maven packaging.

Load Time Weaving – byte code is modified when classes are loaded by class loaders, during class loading aspects are applied, code is modified at join points matching pointcuts by applying advices.

Runtime Weaving – used by Spring AOP, for each object/bean subject to aspects, a proxy object is created (JDK Proxy or CGLIB Proxy), proxy objects are used instead of the original object, at each join point matching pointcut, the method invocation is changed to apply code from advice.