Deep Dive into Spring AOP: Core Classes, Annotation Processing, and Proxy Generation
This article provides a comprehensive analysis of Spring AOP's core proxy mechanisms, detailing the roles of advisor, advice, and pointcut classes, the annotation‑driven configuration process, and how Spring Boot automatically creates proxies using JDK or CGLIB based on configuration flags.
Aspect‑Oriented Programming (AOP) is essential in the Spring framework for implementing cross‑cutting concerns such as transactions, caching, and security. This article performs a deep source‑code analysis of Spring AOP, focusing on three major stages: creating proxy‑related core classes, scanning the container to build PointcutAdvisor objects, and generating the final proxy class.
1. Core Proxy Classes in Spring AOP
The core classes are divided into three groups: AbstractAutoProxyCreator and its subclasses (e.g., AnnotationAwareAspectJAutoProxyCreator) represent different proxy mechanisms. They extend BeanPostProcessor and BeanFactoryAware to hook into the bean lifecycle. Advisor encapsulates a Pointcut and an Advice, linking a cross‑cutting rule with the actual enhancement method. Advice implements the concrete behavior (before, after, around, etc.) that will be applied to matched join points.
UML diagrams (omitted here) illustrate the inheritance hierarchy, showing that AbstractAutoProxyCreator is the most central class for AOP extensions.
2. Annotation‑Based AOP in Spring Boot
Spring Boot’s auto‑configuration loads spring-boot-starter-aop, which registers AnnotationAwareAspectJAutoProxyCreator via spring.factories. The creator reads @EnableAspectJAutoProxy and configures two key flags: spring.aop.auto (default true) enables automatic AOP configuration. spring.aop.proxy-target-class (default true in Spring Boot 2.x) determines whether class‑based (CGLIB) or interface‑based (JDK) proxies are used.
The registration process creates a BeanDefinition for the creator but does not instantiate it immediately. Instantiation occurs later when the bean post‑processor runs.
2.1 Creating the AnnotationAwareAspectJAutoProxyCreator
The AopAutoConfiguration class contains two inner configuration classes—one for JDK proxies and one for CGLIB. Depending on the value of proxyTargetClass, the appropriate configuration is activated.
2.2 Scanning for Aspects and Building Advisors
During the bean creation phase, postProcessBeforeInstantiation checks each bean to see if it should be proxied. If a bean is eligible, the creator registers a BeanDefinition for the aspect and sets up the proxy.
The method findCandidateAdvisors collects all Spring advisors and then adds advisors generated from @Aspect classes via ReflectiveAspectJAdvisorFactory. For each method in an @Aspect class, getAdvisor creates an InstantiationModelAwarePointcutAdvisorImpl that holds the pointcut expression and the corresponding advice implementation.
Advice creation is driven by the annotation type: @Before →
AspectJMethodBeforeAdvice @After→
AspectJAfterAdvice @AfterReturning→
AspectJAfterReturningAdvice @AfterThrowing→
AspectJAfterThrowingAdvice @Around→ AspectJAroundAdvice These advice objects receive the target method, the parsed pointcut, and a reference to the aspect instance factory, enabling lazy instantiation of the aspect.
3. Proxy Generation
After advisors are assembled, postProcessAfterInitialization decides whether to wrap the bean with a proxy. The method wrapIfNecessary checks several conditions:
If the bean is marked as infrastructure or should be skipped, no proxy is created.
Otherwise, getAdvicesAndAdvisorsForBean retrieves applicable advisors via findEligibleAdvisors, which filters advisors using AopUtils.canApply (matching pointcut expressions against the bean’s methods).
When a proxy is needed, createProxy builds an AopProxy implementation. The choice between JDK dynamic proxies and CGLIB proxies follows these rules:
If optimize or proxyTargetClass is true, or if no user‑supplied interfaces are present, CGLIB is used.
Otherwise, JDK proxies are created for the specified interfaces.
In Spring Boot 2.x, proxy-target-class defaults to true, meaning class‑based (CGLIB) proxies are used for all beans except those explicitly proxied by interface.
4. Differences Between Spring Boot 1.x and 2.x
The auto‑configuration class AopAutoConfiguration changed its matchIfMissing attribute:
1.x: matchIfMissing = false – proxy-target-class had to be set explicitly.
2.x: matchIfMissing = true – the flag defaults to true, making CGLIB the default proxy mechanism.
This shift simplifies configuration but also means developers must be aware that class‑based proxies are now the norm.
5. Summary
Spring Boot automatically enables AOP through AopAutoConfiguration, registers AnnotationAwareAspectJAutoProxyCreator, scans for @Aspect classes, builds advisors, and finally creates either JDK or CGLIB proxies based on configuration flags. Understanding the flow—from core proxy classes, through annotation processing, to proxy generation—helps developers troubleshoot AOP behavior and fine‑tune performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
