Open-Source Wikis

/

Spring Framework

/

Modules

/

spring-aspects

spring-projects/spring-framework

spring-aspects

Active contributors: Juergen Hoeller, Sam Brannen

Purpose

spring-aspects provides AspectJ-language aspects that implement Spring's annotation-driven cross-cutting concerns (@Transactional, @Async, @Cacheable, @Configurable) without runtime proxies. These aspects are designed for compile-time weaving (CTW) and load-time weaving (LTW) — the bytecode of target classes is rewritten so calls to advised methods are intercepted in place. This sidesteps the limitations of proxy-based AOP (self-invocation, final classes, non-public methods).

Directory layout

spring-aspects/
├── spring-aspects.gradle           # io.freefair.aspectj plugin configuration
└── src/
    ├── main/
    │   ├── aspectj/org/springframework/   # *.aj aspect sources
    │   └── java/org/springframework/      # *.java helpers
    └── test/

The build applies io.freefair.aspectj rather than the standard Java plugin and configures compileAspectj for source/target Java 17.

Key aspects shipped

Aspect Purpose
AnnotationTransactionAspect Implements @Transactional via weaving (alternative to AOP proxies)
JtaAnnotationTransactionAspect JTA-specific variant
AnnotationAsyncExecutionAspect Implements @Async via weaving
AnnotationCachingAspect Implements @Cacheable/@CachePut/@CacheEvict via weaving
AnnotationBeanConfigurerAspect Implements @Configurable — auto-injects dependencies into objects created with new

How it works

graph LR
    SRC[Your @Transactional class.java] -->|"compile + AspectJ weaver"| WOVEN[Bytecode with inlined transactional logic]
    WOVEN -->|"runs"| JVM[JVM]

    OR[Or use Load-Time Weaving:]
    SRC2[Your @Transactional class.java] -->|"compile"| CLASS[Standard .class file]
    CLASS -->|"loaded"| AGENT[spring-instrument agent]
    AGENT -->|"weaves"| WOVEN2[Modified bytecode]
    WOVEN2 -->|"runs"| JVM2[JVM]

Compile-time weaving

The application's build (Gradle/Maven AspectJ plugin) runs the AspectJ compiler against both your code and spring-aspects.jar. The output .class files have transactional/async/cache logic inlined.

Load-time weaving

spring-instrument's agent is loaded at JVM startup. As classes are loaded, the agent inspects them and applies the aspects from spring-aspects.jar. Configured via @EnableLoadTimeWeaving and META-INF/aop.xml.

When to use this instead of spring-aop

Concern Choose spring-aop (proxy) Choose spring-aspects (weaving)
Setup complexity Just add @EnableTransactionManagement Requires AspectJ compiler or LTW agent
Self-invocation of advised methods ❌ Bypassed ✅ Works
final classes / methods ❌ Cannot be advised (CGLIB limit) ✅ Works
Non-public methods Limited Full access
@Configurable (dep-inject new-created objects) ❌ Not supported ✅ Required
Native-image (GraalVM) friendliness Better More complex

The default for most apps is proxy-based AOP from spring-aop. Reach for spring-aspects when proxies don't fit.

Integration points

  • Depends on spring-context (for @Transactional, @Async, @Cacheable annotations).
  • Companion to spring-instrument for LTW.
  • Not a transitive dependency of typical Spring applications — it's opt-in.

Entry points for modification

  • New cross-cutting concern that needs CTW/LTW: write a new *.aj aspect alongside the existing ones in spring-aspects/src/main/aspectj/.
  • Adjusting weaving rules: most logic is per-aspect; the compileAspectj configuration is in spring-aspects.gradle.

See also

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

spring-aspects – Spring Framework wiki | Factory