Repaso
Español
Design & PracticesIntermediateDP-47

Which pattern describes Spring wrapping your bean to add transactions, caching or security without changing its code?

Options

  1. Proxy (Correct option)
  2. Singleton
  3. Builder
  4. 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.

Key terms

Practice this question in Repaso