Spring AOP

AOP’s essence: Enhancing cross-cutting logic without changing the original logic. Cross-cutting logic code is often permission checking code, logging code, transaction control code, and performance monitoring code.

Spring AOP (Aspect-Oriented Programming) is part of the Spring framework providing proxy-based AOP functionality. It allows you to add additional features or behavior to your application without changing the original code.

Core Concepts

Aspect

Aspect is the core concept of AOP, representing the modularization of cross-cutting concerns. An aspect consists of pointcuts and advice. An aspect defines which methods will apply additional functionality (advice).

Joinpoint

A Joinpoint represents a point in program execution, such as method call, method execution, constructor call, etc. Joinpoints in Spring AOP are usually where method execution occurs.

Pointcut

A Pointcut defines which Joinpoints need to be intercepted. It is usually based on method signature filtering. For example, select all methods in a certain package, or methods of a certain class.

Advice

Advice defines the code to execute at a Joinpoint. Advice determines the specific behavior of enhancement. Advice has different types:

  • Before Advice: Executes before method execution
  • After Advice: Executes after method execution, regardless of whether the method throws an exception
  • AfterReturning Advice: Executes after method completes normally
  • AfterThrowing Advice: Executes when method throws an exception
  • Around Advice: Can control method execution, can choose to execute or not execute the method, or modify the method’s return value

Target Object

The Target Object is the object being proxied by AOP. It is the actual object of aspect operations.

How Spring AOP Works

Spring AOP is implemented based on the proxy pattern. It mainly generates proxy objects through two methods:

  • JDK Dynamic Proxy: Applicable when the target object implements an interface
  • CGLIB Proxy: Applicable when the target object does not implement any interface

AOP Terminology

Business Main Line

Before explaining AOP terminology, let’s look at these diagrams. The above diagram describes a program design that does not use AOP. When we circle the methods in the red box, it brings a lot of repetitive work. The program is filled with a large amount of duplicate code. In the diagram below using AOP design, it extracts the code in the red box and uses dynamic proxy technology to enhance the business logic methods that need to be used at runtime.

AOP Terminology Details

  • JoinPoint: It refers to points that can be used to add enhanced code to the business main line. These points refer to methods. Enhanced code is added through dynamic proxy technology before and after method execution. In Spring framework’s AOP implementation, only method-type Joinpoints are supported.
  • Pointcut: It refers to the Joinpoint after enhanced code has been added to the business main line. From the above diagram, we can see the transfer method in the presentation layer is just a Joinpoint because the access permission check functionality did not enhance it.
  • Advice (Enhancement): It refers to methods in an aspect that provide enhancement functionality. Different methods have different enhancement timings. For example, starting a transaction must be before the business method executes, committing a transaction must be after the business method executes, and rollback must be executed when the business method errors occur. These are advice types. Current classifications: Before advice, After advice, After-throwing advice, After advice, Around advice.
  • Target: It refers to the proxy’s target object, i.e., the proxied object
  • Proxy: It refers to the class that produces the proxy class after AOP weaves enhancement, i.e., the proxy object.
  • Weaving: It refers to the process of applying enhancement to the target object to create a new proxy. Spring uses dynamic proxy weaving, while AspectJ uses compile-time weaving and class-loading-time weaving.
  • Aspect: It refers to the aspect that enhanced code concerns. Defining these related enhancement codes into a class is the aspect.

AOP Proxy Selection

Spring uses dynamic proxy technology to implement AOP. By default, Spring selects between JDK or CGLIB based on whether the proxied object implements an interface. When the proxied object implements an interface, Spring selects JDK’s official proxy technology. However, we can configure Spring to forcibly use CGLIB.


AOP Configuration Methods

In Spring’s AOP configuration, like IoC configuration, it supports three types of configuration:

  • Using XML configuration
  • Using XML + annotation configuration
  • Using pure annotation configuration

AOP Implementation

Requirement Description

Cross-cutting logic code is logging. The logging logic is woven into the target method’s specific position.

Add Dependencies

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-aop</artifactId>
 <version>5.1.12.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.9.4</version>
</dependency>

Core Configuration

The steps for XML-based configuration are as follows:

  1. Give the advice Bean to Spring for management
  2. Use aop:config to start AOP configuration
  3. Use aop:aspect to configure the aspect
  4. Use corresponding tags to configure advice types
<!-- Log utility managed by Spring -->
<bean id="wzkLogUtils" class="wzk.utils.WzkLogUtils"></bean>
<!-- AOP Configuration -->
<aop:config>
    <!-- Configure aspect -->
    <aop:aspect id="wzkLogAdvice" ref="wzkLogUtils">
        <aop:before method="printLog" pointcut="execution(public * wzk.service.impl.WzkTransferServiceImpl.update(wzk.model.WzkAccount))"></aop:before>
    </aop:aspect>
</aop:config>

Pointcut Expression

Pointcut expression is also called AspectJ pointcut expression. It refers to a string following a specific syntax structure. Its function is to enhance Joinpoints that meet the syntax format. It is part of AspectJ.

Change Proxy Configuration

When changing proxy configuration, Spring selects based on the actual situation of the proxied object when choosing to create a proxy. If the proxied object implements an interface, it uses interface-based dynamic proxy. When the proxied object does not implement any interface, Spring automatically switches to subclass-based dynamic proxy.

There are two configuration methods:

  • Use aop:config tag to configure proxy-target-class=“true”
  • Use aop:aspectj-autoproxy tag to configure proxy-target-class=“true”