Interview Prep Zoneby CuriouserLabs

Question 1 of 5 · senior level

How does Spring AOP actually work? What happens when I call a method on an @Transactional bean?

🎤 Say this first

Spring AOP is proxy-based: during bean initialization, a BeanPostProcessor checks whether any aspect's pointcut matches the bean; if so, the context gets a proxy (JDK dynamic proxy for interfaces, CGLIB subclass otherwise) wrapping the target. Callers hold the proxy; each external call runs the interceptor chain (advice) around the delegated target call. Consequence bundle: only public external calls are advised, self-invocation bypasses everything, and interception is per-method-boundary, not per-line.

The full picture

🎬 AOP: how the proxy intercepts a call

1 / 5
ControllerorderService.place()Proxy (what got injected) 🎁before adviceOrderService (target)place() — your codeafter advicecallInterface → JDK dynamic proxy; class → CGLIB subclass. Either way: interception happens only at the proxy boundary.

The controller injects OrderService — but Spring actually injected a PROXY that wraps the real object. Calls to orderService.place() hit the proxy first.

Caller → proxy → before advice → target → after advice — and the self-invocation trap.
  • Where proxies are born: postProcessAfterInitialization in the auto-proxy creator — late enough that the proxy wraps the fully initialized bean.
  • Weaving spectrum: Spring AOP = runtime proxies (no bytecode modification); AspectJ = compile-time or load-time weaving (modifies the class itself — can advise private methods, field access, constructors, self-calls).
  • @Transactional flow: proxy → TransactionInterceptorPlatformTransactionManager.getTransaction() (begin/join per propagation) → target method → commit, or rollback on rule-matching exception.
  • Ordering: multiple aspects nest by @Order — e.g., security outside transaction outside retry, and getting that order wrong is a real production bug (retrying inside a doomed transaction).

🔄 Likely follow-up questions

  • Why does the proxy get created in after-initialization rather than before?
  • What's the difference between an advisor, an advice and an aspect?
  • How would you see the proxy in a debugger / verify a method is actually advised?