An in-depth explanation of Spring AOP

Chapter 1: Introduction

Hello everyone, I am Xiaohei. What we are going to talk about today is AOP (aspect-oriented programming) of the Spring framework in Java. For programmers, understanding AOP is super critical to mastering the Spring framework. It is like magic, allowing us to add various functions to the program without changing the original code.

AOP is not just a programming paradigm, it is an idea. In the Spring framework, the benefits brought by AOP include but are not limited to code decoupling and reuse. Imagine that if there is a piece of logic that needs to be reused in many places, such as logging and permission verification, a lot of repeated code may be written using the traditional OOP (object-oriented programming) method. AOP is a powerful tool used to solve such problems.

AOP allows us to extract these common functions through a method called “aspects” and dynamically apply these functions at different program execution points. This may sound a bit abstract, but don’t worry, we will use examples to explain it in detail.

Chapter 2: AOP Basics

To deeply understand AOP in Spring, we must first understand several basic concepts: Aspect, Join Point, Advice, etc. These concepts are the cornerstones of AOP. Only by understanding these can we better understand how Spring AOP operates.

  • Aspect : This is the core of AOP. You can think of it as an independent module that we want to insert into the application. For example, we can create a log aspect to record the running status of the application.
  • Join Point : This refers to a specific point during program execution, such as a method call or an exception being thrown. In Spring AOP, connection points mainly refer to method calls.
  • Advice : This is an action performed by an aspect at a specific connection point. There are several types of notifications, such as “pre-advice” which is executed before the method is executed, and “post-advice” which is executed after the method is executed.

Let’s take a look at a simple example using Java code to implement a logging aspect. Xiaohei will demonstrate it to you:

import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;

public  class  LogAspect {
     // Execute @Before("execution(* com.example.service.*.*(..))") before executing all methods under the service package 
    public void beforeMethod () {
        System.out.println( "Logging: Method execution begins" );

In this code, @Aspectit means that this is an aspect. Indicates that this is a pre-notification, which will run before @Beforethe specified method (here, all methods of all classes under the package) is executed. com.example.serviceHere execution(* com.example.service.*.*(..))is an expression that specifies the connection point for the notification application.

Whenever we call com.example.serviceany method under the package, the sentence “Logging: Method starts execution” will be printed first. This is the magic of AOP, allowing such functionality to cross-cut the entire application without modifying any business logic code.

Let’s talk more deeply about the relationship between AOP and OOP. In OOP, we solve problems through encapsulation, inheritance and polymorphism, emphasizing the concepts of objects and classes. AOP is a lateral way of thinking, which allows us to break out of these traditional thinking modes and deal with problems from another angle. Through AOP, we can enhance or modify the behavior of the program without touching the main business logic.

There is a key point to understand here. AOP is not meant to replace OOP, but to complement OOP. In actual development, we often use OOP to build business models, and then use AOP to solve cross-cutting concerns (such as logging, security, transaction management, etc.), so that we can write cleaner and easier to maintain code.

AOP provides Java programmers with a powerful tool to make code more modular and separate concerns. Mastering AOP, when we use the Spring framework, we can build and optimize our applications as we like like playing with Lego blocks.

Chapter 3: AOP implementation in Spring

Let’s continue to delve deeper into Spring and talk about how Spring implements AOP. Spring AOP is designed around the proxy pattern. The proxy mode here actually refers to using a proxy object to control access to the original object. This proxy object adds some additional functions based on the original object.

In Spring AOP, two proxy methods are mainly used: JDK dynamic proxy and CGLIB proxy.

JDK dynamic proxy

JDK dynamic proxy is mainly used for interface proxy. By implementing the methods in the interface, it can execute the logic defined in the aspect when called. Xiaohei will show it to everyone in code:

import java.lang.reflect.Proxy;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;

public  class  JdkDynamicProxy  implements  InvocationHandler {
     private Object target; // The target object of the proxy

    public  JdkDynamicProxy (Object target) {
         this .target = target;

    public Object getProxy () {
         return Proxy.newProxyInstance(
            target.getClass().getClassLoader(), // Class loader 
            target.getClass().getInterfaces(), // Get the interface of the target object 
            this ); // InvocationHandler implementation

    public Object invoke (Object proxy, Method method, Object[] args)  throws Throwable {
        System.out.println( "Processing before method invocation" );
         Object  result  = method.invoke(target, args); //Invoke the method of the target object 
        System.out.println( "Processing after method invocation" );
         return result;

In this example, JdkDynamicProxythe class implements InvocationHandlerthe interface. When any method of the proxy object is called, it is forwarded to invokethe method. Here, we have added some additional processing before and after the method call. This is the essence of AOP.


If the target object does not implement the interface, Spring will use CGLIB to generate a subclass to act as a proxy. CGLIB proxy is more powerful than JDK dynamic proxy. It can implement proxy without interface. This approach is typically used for proxy classes rather than interfaces.

import org.springframework.cglib.proxy.Enhancer;
 import org.springframework.cglib.proxy.MethodInterceptor;
 import org.springframework.cglib.proxy.MethodProxy;

public  class  CglibProxy  implements  MethodInterceptor {
     private Object target;

    public Object getProxy (Object target) {
         this .target = target;
         Enhancer  enhancer  =  new  Enhancer ();
        enhancer.setSuperclass( this .target.getClass()); // Set the proxy target 
        enhancer.setCallback( this ); // Set the callback 
        return enhancer.create(); // Create the proxy object

    public Object intercept (Object obj, Method method, Object[] args, MethodProxy proxy)  throws Throwable {
        System.out.println( "Processing before method invocation" );
         Object  result  = proxy.invokeSuper(obj, args); // Call the method of the target object 
        System.out.println( "Processing after method invocation" );
         return result;

In this code, we use Spring Enhancerclasses to create proxy objects. The method is similar to the method interceptin the JDK dynamic proxy . It is the processing point for method calls.invoke

Whether it is a JDK dynamic proxy or a CGLIB proxy, their core idea is to add additional processing logic on the basis of the original object. This is the essence of Spring AOP’s implementation mechanism.

Next, let’s talk about how Spring AOP works. In the Spring framework, when a Bean is defined as an aspect, Spring will dynamically apply this aspect to the target Bean at runtime. This process is achieved by creating a Bean proxy. When a bean method is called, the method of the proxy object is actually called. This proxy object will decide whether to perform additional operations based on the defined aspect logic, such as calling pre-notification or post-notification.

Let’s understand this process through an actual Spring AOP example. Suppose we have a simple service class and need to add logs before and after calling its methods:

public  class  SimpleService {
     public  void  performTask () {
        System.out.println( "Execute business logic" );

Now, let’s define a log aspect:

import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.After;

public  class  LoggingAspect {
     @Before("execution(* SimpleService.performTask(..))") 
    public  void  logBefore () {
        System.out.println( "Before method execution: record log" );

    @After("execution(* SimpleService.performTask(..))") 
    public  void  logAfter () {
        System.out.println( "After method execution: record log" );

In this aspect, @Beforeand @Afterannotations define pre-advice and post-advice. They run before and after the method SimpleServiceof the class performTaskis executed.

When the Spring framework loads this configuration, it SimpleServicecreates a proxy for the class. This proxy will performTaskfirst call the method when the method is called logBefore, then execute the original performTaskmethod, and finally call logAfterthe method.

In this way, Spring AOP allows

Allows us to enhance existing code functionality in a non-intrusive way. This dynamic proxy method makes aspect applications flexible while maintaining code clarity and maintainability.

However, Spring AOP also has its limitations. For example, it can only be applied to beans managed by the Spring container. This means that if your object is not a Spring-managed bean, Spring AOP cannot proxy and enhance it. Also, since it is proxy-based, it cannot be applied to non-public methods or methods called inside methods.

Despite these limitations, Spring AOP is still a powerful and flexible tool, especially suitable for handling cross-cutting concerns such as logging, transaction management, security control, etc. By abstracting these concerns away from business logic, we can write more concise and reusable code. Moreover, the configuration and use of Spring AOP are relatively simple, allowing us to focus more on the implementation of business logic.

So far, we have discussed the basic concepts, implementation methods and working principles of Spring AOP. Through this knowledge, we can better understand the application of AOP in the Spring framework, and thus use Spring more efficiently to build complex enterprise-level applications. In the next chapters, we will delve into the key components and advanced features of Spring AOP, so stay tuned!

Chapter 4: Key components of Spring AOP

In Spring AOP, there are several key components that we must understand: Aspect, Advice, and Pointcut. These components are the building blocks on which AOP functionality is built. Let Xiao Hei take you to find out.


Aspects are the core of AOP, which combine advice and pointcuts to provide a complete modularization of concerns. This is like a container that contains specific functions, such as logging, transaction management, etc. Aspects define “what” and “when” these functions are performed.

import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;

public  class  LogAspect {
     // A pre-notification is defined here 
    @Before("execution(* com.example.service.*.*(..))") 
    public  void  beforeMethod () {
        System.out.println( "Logging: Method execution begins" );

The class here LogAspectis an aspect, which is @Aspectidentified by annotations. The method inside beforeMethodis a pre-notification, used to print the log before the target method is executed.


Advice defines the specific behavior of the aspect. In Spring AOP, there are five types of notifications:

  1. Before advice : executed before the target method is executed.
  2. After advice : executed after the target method is executed, regardless of whether the method executes successfully or exits abnormally.
  3. After-returning advice : Executed after the target method executes successfully.
  4. Exception advice (After-throwing advice) : Executed after the target method throws an exception.
  5. Around advice : You can customize the code to be executed before and after the target method, and you need to manually execute the target method.
import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;

public  class  PerformanceAspect {
     // Around notification example 
    @Around("execution(* com.example.service.*.*(..))") 
    public Object profile (ProceedingJoinPoint pjp)  throws Throwable {
         long  start  = System.currentTimeMillis ();
         Object  output  = pjp.proceed(); // Execute the target method 
        long  elapsedTime  = System.currentTimeMillis() - start;
        System.out.println( "Execution time: " + elapsedTime + "milliseconds" );
         return output;


Pointcuts define the methods on which advice should be executed. By specifying it via an expression, you can very precisely control where notifications are applied. Pointcut expressions define “where” these functions are performed.



public  class  AuditAspect {

    // Define pointcut expression 
    @Pointcut("execution(* com.example.service.*.*(..))") 
    public  void  serviceMethods () {
         // No code implementation is required here

    // Use the defined entry point 
    public  void  beforeServiceMethod () {
        System.out.println( "Preparations before the audit starts" );

In this case, serviceMethodsis a pointcut that specifies that the advice will com.example.servicebe executed on all methods of all classes under the package. The methods then beforeServiceMethodact as pre-notifications, which will be executed before those methods are executed.

Through the combination of these components, Spring AOP allows us to handle concerns that span the entire application in a very flexible and powerful way. Whether it is logging, security control, transaction management or performance monitoring, it can all be easily achieved by defining the appropriate aspects, notifications and entry points.

To summarize, aspects are where advice and pointcuts are combined, defining when and where to perform what operations. Advice describes the specific behavior of an aspect, while pointcuts specify exactly where these behaviors should occur. This is the magic of Spring AOP. Through the combination of these elements, we can easily add functionality across different modules and levels to the application without modifying the actual business logic code.

Chapter 5: Practical combat: Using Spring AOP to implement logging

Now we have come to the most exciting part. Xiaohei will take you to practice how to use Spring AOP to implement logging. Logging is a very common function in development. It can help us monitor the running status of the application and analyze the cause of the problem. Using AOP to implement logging can make the code more concise and easier to maintain.

Define aspects of logging

First, we need to define an aspect to be responsible for logging. This aspect will contain a before advice (Before advice) to log before the method is executed, and an after advice (After advice) to log after the method is executed.

import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.reflect .MethodSignature;

public  class  LoggingAspect {

    // Pre-notification: call @Before("execution(* com.example.service.*.*(..))") 
    before method execution public  void  logBefore (JoinPoint joinPoint) {
         MethodSignature  signature  = (MethodSignature) joinPoint. getSignature();
         String  methodName  = signature.getMethod().getName();
        System.out.println( "Start executing method: " + methodName);

    // Post notification: call 
    @After("execution(* com.example.service.*.*(..))") after the method is executed 
    public  void  logAfter (JoinPoint joinPoint) {
         MethodSignature  signature  = (MethodSignature) joinPoint. getSignature();
         String  methodName  = signature.getMethod().getName();
        System.out.println( "Method execution completed: " + methodName);

In this example, the logBeforeand logAftermethods print logs before and after the target method is executed. via `JoinPoint

`Object, we can get the detailed information of the executing method, such as the method name, so that we can clearly show which method is executing in the log.

Configuration and application aspects

After defining the aspect, we need to apply it to our application. In the Spring framework, this usually means some configuration is required. We can achieve this through annotations or XML configuration.

If we are using annotation-based Spring configuration, we only need to simply add @EnableAspectJAutoProxyannotations to the configuration class, so that Spring will automatically recognize @Aspectthe annotated class as an aspect.

import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.EnableAspectJAutoProxy;

public  class  AppConfig {
     // Other Beans can be defined here 

Now that our aspects are ready, we need to create some service classes to simulate real business scenarios and see how AOP works.

Create a sample service class

package com.example.service;

public  class  OrderService {
     public  void  createOrder () {
         // Here is the logic for creating an order 
        System.out.println( "Create Order" );

    public  void  cancelOrder () {
         // Here is the logic of canceling the order 
        System.out.println( "Cancel Order" );

In this OrderServiceclass, we define two methods: createOrderand cancelOrder. When these methods are called, our log aspect should be able to capture these calls and print the corresponding logs before and after the method is executed.


Through this simple example, we saw the application of Spring AOP in actual development. Using AOP to implement logging not only makes the code more concise, but also improves the maintainability and reusability of the code. We don’t need to manually add logging code in each method, but manage these cross-cutting concerns through a centralized aspect.

This is the magic of Spring AOP, which allows us to handle cross-cutting concerns in our application in a very elegant and flexible way. As our understanding of Spring AOP deepens, we will be able to use this powerful tool more efficiently to build and maintain complex enterprise-level applications.

Chapter 6: Advanced Features of Spring AOP

We have already looked at the basic applications of Spring AOP, and now Xiaohei will take you to dig deeper into its advanced features. In this chapter, we will explore two advanced concepts in Spring AOP: Introduction and Enhancement, as well as how to integrate AspectJ with Spring AOP to implement more complex AOP scenarios.


Introduction is a powerful feature of AOP, which allows us to add new methods and properties to existing classes. This is very useful for enhancing the functionality of a class without modifying the source code. Let’s look at an example:

Suppose we have an PaymentServiceinterface and its implementation class PaymentServiceImpl. Now, we want to add a new feature to this class, such as logging. However, if we don’t want to add this function to the existing class or interface, we can use introduction to achieve it.

First, define an interface containing logging methods:

public  interface  LoggingCapability {
     void  enableLogging () ;

Then, create an implementation of this interface:

public  class  LoggingIntroduction  implements  LoggingCapability {
     private  boolean  loggingEnabled  =  false ;

    public  void  enableLogging () {
        loggingEnabled = true ;
        System.out.println( "Logging is enabled" );

    // Used to check whether logging is enabled 
    public  boolean  isLoggingEnabled () {
         return loggingEnabled;

Now, use the import feature of Spring AOP to add this functionality to PaymentService:

import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.DeclareParents;

public  class  LoggingIntroductionAspect {

    @DeclareParents(value = "com.example.service.PaymentServiceImpl", defaultImpl = LoggingIntroduction.class) 
    public  static LoggingCapability loggingCapability;

Using @DeclareParentsannotations, we successfully PaymentServiceImpladded logging functionality to the class without changing its source code.

Enhancement (Advisor)

In Spring AOP, an enhancement (or advisor) is a notification that applies to a specific pointcut. It is one of the core components in AOP and is used to define the behavior of aspects. The main function of enhancement is to apply notifications to beans that meet specific conditions.

Let’s look at an example of using augmentation. Suppose we want to saveuse transaction management on all service class methods. At this time, we can define an enhancement to achieve this goal:

 import org.springframework.transaction.interceptor.TransactionInterceptor;

public  class  TransactionAdvisor {

    public NameMatchMethodPointcutAdvisor getAdvisor (TransactionInterceptor transactionInterceptor) {
         NameMatchMethodPointcutAdvisor  advisor  =  new  NameMatchMethodPoint

        advisor.setMappedName( "save*" );
         return advisor;

In this example, NameMatchMethodPointcutAdvisora pointcut is defined that will match any savemethod that begins with . We then TransactionInterceptorapply (transaction interceptors) as advice to these pointcuts. This way, all matching savemethods automatically have transaction management applied when executing.

Integration of AspectJ and Spring AOP

AspectJ is a powerful AOP framework that provides more AOP capabilities and control than Spring AOP. In Spring, we can combine AspectJ’s AOP function with Spring AOP to implement more complex AOP scenarios.

For example, we can use AspectJ annotations to define aspects and then manage these aspects in Spring. The advantage of this is that we can take advantage of AspectJ’s powerful pointcut expression language while enjoying the dependency injection and AOP management provided by Spring.

Let’s look at an example:

import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
 import org.aspectj.lang.annotation.Before;

public  class  AuditAspect {

    // Define a pointcut 
    @Pointcut("execution(* com.example.service.*.*(..))") 
    public  void  serviceLayer () {

    //Execute @Before("serviceLayer()") 
    before each method in the service layer public  void  logServiceAccess () {
        System.out.println( "Access Service Layer" );

In this example, AuditAspectit is an aspect defined using AspectJ annotations. It will com.example.serviceperform logging operations before all methods of all classes under the package are executed.

By integrating AspectJ and Spring AOP, we can obtain the richer AOP features of AspectJ while maintaining the ease of use of Spring. This provides more flexibility and power for handling complex AOP scenarios.

So far, we have discussed some advanced features of Spring AOP, including introduction, enhancement, and how to integrate AspectJ with Spring AOP. These advanced features provide us with powerful tools to deal with complex programming challenges, allowing us to develop high-quality Java applications more flexibly and efficiently.

Chapter 7: Performance and Best Practices

Xiaohei is here to talk with you about the performance considerations and some best practices of Spring AOP. When using Spring AOP, performance is a topic that cannot be ignored. Although Spring AOP provides powerful functionality and flexibility, it can also have a negative impact on application performance if used improperly. We’ll also explore some best practices to ensure our applications are both efficient and robust.

Performance considerations for Spring AOP

Spring AOP is proxy-based, which means that every time we call a proxy method, there will be additional performance overhead. This is mainly due to the need to perform additional logic, such as aspect notifications. While this overhead won’t be too noticeable in most cases, it can become an issue in high-performance and high-concurrency scenarios.

In order to minimize the performance impact, we can take the following measures:

  • Precise pointcut definition : Ensure that pointcuts are as precise as possible to avoid unnecessary method calls being proxied. Using more specific pointcut expressions can reduce the scope of AOP.
  • Avoid complex aspect logic : The logic in aspects should be kept as simple and efficient as possible. Complex or time-consuming operations increase the overhead of each method call.
  • Reasonable use of notification types : For example, if you only need to perform operations before a method is executed, you should not use surrounding notifications, because surrounding notifications will bring more performance overhead.

Best Practices

In addition to focusing on performance, following some best practices can also help us better use Spring AOP:

  1. Separation of concerns : Aspects should only focus on a specific functionality, such as logging, security, or transaction management. This not only improves the readability of the code, but also facilitates maintenance and testing.
  2. Use AspectJ annotations with caution : Although AspectJ provides powerful pointcut expressions, overuse or improper use can lead to code that is difficult to understand and maintain. Where possible, prefer using Spring’s @Transactionaland @Cacheablesuch annotations.
  3. Optimize the scope of Spring Bean : When defining a bean, consider the impact of its scope on performance. For example, singleton-scoped beans have better performance than prototype-scoped beans.
  4. Document and maintain aspects : As your application grows, aspects may become more complex. Good documentation and maintenance are critical to maintaining AOP logic over the long term.
  5. Testing and Validation : AOP may inadvertently change the behavior of a program. Therefore, conduct thorough testing

And validation is very important to ensure that aspects behave as expected and do not introduce any unintended side effects.

  1. Proper exception handling : properly handle exceptions in aspect logic to ensure that exceptions do not cause unexpected interruption of program flow. Especially in surround advice, make sure to handle the target method’s return value and exceptions correctly.
  2. Avoid circular dependencies : When defining aspects, be careful not to create circular dependencies, especially when aspects and business beans depend on each other. This may cause Spring container initialization to fail.
  3. Use conditional aspects : In some cases, not all environments need to perform aspect logic. Using conditional aspects (such as through configuration switches) can improve the flexibility and performance of your application.

By following these best practices, we can ensure that when using Spring AOP, we can take full advantage of its powerful features while maintaining high performance and good architecture of the application. Remember, while AOP provides great convenience and power, it is also a tool that needs to be used with caution. Correct use of Spring AOP can help us build more robust, maintainable and efficient Java applications.

Chapter 8: Summary

AOP’s place in modern Java applications

AOP has become an integral part of modern Java applications. It greatly improves the maintainability and reusability of your code by providing an elegant way to handle cross-cutting concerns such as logging, transaction management, etc. In the Spring framework, AOP is widely used in various enterprise-level applications, from simple web applications to complex microservice architectures.

Through AOP, developers can separate business logic from system services, making the system more modular. This separation not only makes the code easier to understand and maintain, but also makes unit testing and mock testing simpler.

Future trends and possible development directions of AOP

With the rise of microservices and cloud-native applications, AOP application scenarios have become more extensive. AOP plays an important role in microservice architecture, such as service invocation, load balancing, circuit breaker mode, etc.

With the popularity of reactive programming and non-blocking programming, AOP is gradually adapting to these new programming paradigms. For example, for reactive frameworks like Spring WebFlux, AOP needs to be able to handle asynchronous and non-blocking operations.

In the future, we can foresee that AOP will be combined with artificial intelligence, machine learning and other fields to provide more flexible and powerful underlying support for these advanced technologies.