Proxy Pattern, JDK Dynamic Proxy vs CGLIB Proxy, and How Spring Chooses
Learn Proxy Pattern, JDK Dynamic Proxy vs CGLIB Proxy, and How Spring Chooses (Phase 11 · Spring Proxies (Essential), Spring Core) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
The proxy pattern in Spring • A proxy is a stand-in object with the same type as the target that intercepts calls , adds behaviour (transaction, security, cache, async, lazy loading) and delegates to the real target . Spring creates proxies during bean post-processing ( postProcessAfterInitialization of AbstractAutoProxyCreator ) and puts the proxy in the container, so every injection point receives the proxy. • Two mechanisms: JDK dynamic proxy ( java.lang.reflect.Proxy , implements the target's interfaces ) and CGLIB (generates a subclass of the target class at runtime; Spring repackages it...