Java Spring AOP
The mechanism behind @Transactional, @Cacheable, @Async and @ControllerAdvice. Knowing it is proxying explains most Spring surprises at once.
3 concepts · 7 interview questions · 1 answered in depth
Answered in depth
How does Spring AOP work, and what are its limits?
Spring builds a proxy around your bean — a JDK dynamic proxy if it has an interface, a generated subclass otherwise — and the advice lives in the proxy, not in your class. Every limit follows: self-invoked, private, final and static methods never cross the proxy, so an annotation on them does nothing and nothing warns you.
Asked constantlyintermediate2–10 yrs11 min read
What this topic covers
Every concept in spring aop, and the questions each one gets asked as. Where a question links, it has a full write-up.
Proxies
Spring wraps the bean in a proxy — JDK dynamic if it implements an interface, CGLIB subclass otherwise — and the proxy applies the behaviour.
- How does Spring AOP work, and what are its limits?
- JDK dynamic proxy versus CGLIB — when does Spring pick which?
- Why does a final method break AOP?
Aspects, pointcuts and advice
An aspect bundles a pointcut that selects join points with advice that runs around them.
- What are a join point, a pointcut and advice?
- When would you write a custom aspect rather than use a library one?
@Async and @Cacheable
Two more proxy-backed annotations, with the same self-invocation blind spot and their own configuration traps.
- Why does @Async silently run synchronously sometimes?
- What thread pool does @Async use by default?