spring-projects/spring-framework
spring-instrument
Active contributors: Juergen Hoeller
Purpose
spring-instrument is a Java agent for class instrumentation. It exists to support load-time weaving (LTW) for AspectJ aspects defined in spring-aspects (and any user-supplied ClassFileTransformer). When attached at JVM startup, it makes the JDK's Instrumentation API available to Spring so that classes can be transformed as they are loaded.
Directory layout
spring-instrument/
└── src/main/java/org/springframework/instrument/
├── InstrumentationSavingAgent.java # the agent's premain entrypoint
└── ClassFileTransformer.java # placeholder typeThe module ships two source files, making it among the smallest published Spring artifacts. Its JAR manifest declares Premain-Class and Agent-Class so Java recognizes it as an agent.
Key abstractions
| Type | File | Role |
|---|---|---|
InstrumentationSavingAgent |
spring-instrument/src/main/java/org/springframework/instrument/InstrumentationSavingAgent.java |
Captures Instrumentation instance for later retrieval |
How it works
- Start your JVM with
-javaagent:spring-instrument-X.Y.Z.jar. - The JVM calls
InstrumentationSavingAgent.premain(...)and hands it anInstrumentationreference. - The agent stores that reference in a static field.
- Later, Spring's
LoadTimeWeaver(part ofspring-context) retrieves the reference and registersClassFileTransformers for AspectJ, JPA enhancement, etc.
graph LR
JVM_START["java -javaagent:spring-instrument.jar"] -->|"premain"| Agent[InstrumentationSavingAgent]
Agent -->|"stores Instrumentation"| Static[Static reference]
Static -->|"retrieved by"| LTW[InstrumentationLoadTimeWeaver]
LTW -->|"addTransformer"| AspectJ[AspectJ ClassFileTransformer]
AspectJ -->|"weaves @Transactional, @Async, …"| Classes[Loaded classes]When you need this
- Load-time AspectJ weaving (
@EnableLoadTimeWeaving,META-INF/aop.xml). - JPA bytecode enhancement that requires the JPA
LoadTimeWeaverSPI. - Custom
ClassFileTransformers that need to run early.
If you're not using LTW, you don't need this agent. Most applications don't.
Integration points
- Used by
spring-context'sInstrumentationLoadTimeWeaver. - Companion to
spring-aspects(whose aspects are commonly load-time-woven).
See also
- spring-aspects — what gets woven
- features/aop
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.