Which pattern describes Spring wrapping your bean to add transactions, caching or security without changing its code?
Options
- Proxy (Correct option)
- Singleton
- Builder
- Prototype
Answer
Proxy
Why
A Proxy is a stand-in object with the same interface as the real one. It receives the call first, adds something (a transaction, a security check, logging, lazy loading) and then forwards to the real object.
Spring AOP creates proxies around beans for @Transactional, @Cacheable and @PreAuthorize. Hibernate also uses proxies for lazy-loaded entities.
Remember
Proxy = same interface, controls access, adds behavior.