spring-projects/spring-framework
spring-aop
Active contributors: Juergen Hoeller, Sam Brannen
Purpose
spring-aop provides Spring's runtime, proxy-based Aspect-Oriented Programming. It generates JDK or CGLIB proxies that intercept method calls on managed beans and invoke advice (around, before, after, after-returning, after-throwing). It is the backbone of @Transactional, @Async, @Cacheable, and Spring Security's method-level authorization.
Directory layout
spring-aop/
└── src/main/java/org/springframework/aop/
├── Advisor.java
├── Pointcut.java
├── ClassFilter.java
├── MethodMatcher.java
├── aspectj/ # AspectJ pointcut expression integration
│ ├── annotation/ # @Aspect, @Before, @AfterReturning, …
│ └── …
├── config/ # XML <aop:config> parsing
├── framework/ # Proxy factories: JDK, CGLIB, Advised
│ ├── adapter/ # Adapt MethodInterceptor ↔ advice annotations
│ ├── autoproxy/ # AspectJAutoProxyCreator and friends
│ └── …
├── interceptor/ # ExposeInvocationInterceptor, AsyncExecutionInterceptor
├── scope/ # Scoped proxies
├── support/ # AOP utilities, AOP Alliance shim
└── target/ # TargetSource implementationsKey abstractions
| Type | File | Role |
|---|---|---|
Pointcut |
spring-aop/src/main/java/org/springframework/aop/Pointcut.java |
Predicate matching join points (class + method) |
Advice (AOP Alliance) |
repackaged org.aopalliance.aop.Advice |
Marker for "any advice" |
MethodInterceptor (AOP Alliance) |
org.aopalliance.intercept.MethodInterceptor |
Around-advice SPI |
Advisor |
spring-aop/src/main/java/org/springframework/aop/Advisor.java |
Bundles a pointcut with advice |
ProxyFactory / ProxyFactoryBean |
spring-aop/src/main/java/org/springframework/aop/framework/ |
Programmatic and bean-based proxy creation |
JdkDynamicAopProxy |
spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java |
Interface-based proxy |
CglibAopProxy |
spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java |
Subclass-based proxy |
AspectJAwareAdvisorAutoProxyCreator |
spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator.java |
Discovers advisors from @Aspect beans and wraps targets |
AnnotationAwareAspectJAutoProxyCreator |
spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AnnotationAwareAspectJAutoProxyCreator.java |
Includes annotation-driven advisors |
TargetSource |
spring-aop/src/main/java/org/springframework/aop/TargetSource.java |
Source of the target object (singleton, prototype, pooled, …) |
Advised |
spring-aop/src/main/java/org/springframework/aop/framework/Advised.java |
Interface implemented by all proxies for runtime introspection |
How it works
Proxy creation flow
graph TD
BPP[BeanPostProcessor: AnnotationAwareAspectJAutoProxyCreator] -->|"postProcessAfterInitialization(bean)"| CHECK[Determine matching advisors]
CHECK -->|"none"| RAW[Return original bean]
CHECK -->|"some"| FACT[ProxyFactory.getProxy]
FACT -->|"target has interfaces and !proxy-target-class"| JDK[JdkDynamicAopProxy]
FACT -->|"otherwise"| CG[CglibAopProxy]
JDK -->|"replaces in BeanFactory"| BF[BeanFactory]
CG -->|"replaces in BeanFactory"| BFThe advisors come from two places:
@Aspectbeans whose@Pointcut-annotated methods are extracted byAspectJAdvisorFactory.- Standalone
Advisorbeans registered explicitly.
Method invocation through a proxy
sequenceDiagram
participant Caller
participant Proxy
participant Chain as AdvisorChain
participant Target
Caller->>Proxy: foo()
Proxy->>Chain: build interceptor chain for method
Chain->>Chain: ExposeInvocationInterceptor (puts MethodInvocation in TL)
Chain->>Chain: TransactionInterceptor.invoke (e.g., @Transactional)
Chain->>Chain: ... other matched advice ...
Chain->>Target: target.foo()
Target-->>Chain: return value
Chain-->>Proxy: return value
Proxy-->>Caller: return valueEach interceptor is a MethodInterceptor (AOP Alliance) and decides whether to call invocation.proceed() (continue the chain) or short-circuit.
JDK vs CGLIB
| Aspect | JDK proxy | CGLIB proxy |
|---|---|---|
| Target requirement | Implements ≥ 1 interface | Non-final class |
| Mechanism | Proxy.newProxyInstance per interface set |
Subclass via repackaged CGLIB |
final methods |
N/A (works through interfaces) | Cannot be advised |
| Performance | Slightly faster invocation | Slightly slower invocation, but indistinguishable in practice |
| Default selection | When target has interfaces | When target has no interfaces, or proxyTargetClass=true is set |
@EnableTransactionManagement(proxyTargetClass = true) (and similar @Enable* annotations) force CGLIB.
Limitations of proxy-based AOP
- Self-invocation — Calls from within a proxied method to another method on
thisbypass the proxy. This is a regular source of "my@Transactionaldoesn't work" bugs. - Final methods/classes — Cannot be advised by CGLIB.
- Non-public methods — Not matched by default advisors.
For applications that need to overcome these, spring-aspects provides AspectJ compile-time and load-time weaving alternatives.
Integration points
- Sits between
spring-beansandspring-context.spring-contextis what application code typically uses to enable AOP (@EnableAspectJAutoProxy). - Powers many other modules:
spring-tx(transaction interception),spring-context(@Async,@Cacheable,@Validated), Spring Security, Spring Data. - Imports the AOP Alliance interfaces (which Spring repackages to avoid version conflicts).
Entry points for modification
- Custom advice — Implement
MethodInterceptorand pair with aPointcutin anAdvisor. - Custom pointcut expression — Subclass
StaticMethodMatcherPointcutor use AspectJ pointcuts via@Pointcut("execution(...)"). - Custom proxy —
ProxyFactoryis fully programmable. The framework rarely needs new code here. - Custom auto-proxy creator — Subclass
AbstractAutoProxyCreator. Most subclasses already exist for common cases.
See also
- spring-aspects — compile/load-time AspectJ alternative
- spring-tx —
@Transactionalis implemented as AOP advice - features/aop — the end-to-end AOP story
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.