GoF Patterns in Spring — Reference
This page is a quick-lookup reference for the 23 Gang of Four (GoF) design patterns as they appear inside the Spring framework and the Spring Boot ecosystem. Each table covers one GoF category and maps every pattern to where Spring uses it and a concrete class or annotation you can go read. Use it as a cheat sheet; the dedicated pages in this section explain the headline patterns in depth.
Creational patterns
These govern how objects are created — Spring’s whole reason for existing as an IoC container.
| Pattern | Where Spring uses it | Example class / annotation |
|---|---|---|
| Singleton | Default bean scope (one instance per context) | @Component, @Scope("singleton") |
| Factory Method | Programmatic bean creation | @Bean method, FactoryBean#getObject |
| Abstract Factory | The container that produces all beans | BeanFactory, ApplicationContext |
| Builder | Fluent construction of complex objects | UriComponentsBuilder, RestClient.builder(), Lombok @Builder |
| Prototype | Fresh instance per lookup | @Scope("prototype"), ObjectProvider#getObject |
See Singleton, Factory & Abstract Factory, and Builder.
Structural patterns
These govern how objects are composed and how interfaces are bridged.
| Pattern | Where Spring uses it | Example class / annotation |
|---|---|---|
| Proxy | AOP cross-cutting concerns | @Transactional, @Cacheable, @Async, JDK proxy / CGLIB |
| Adapter | Bridging handler & invocation styles | HandlerAdapter, HandlerInterceptorAdapter |
| Decorator | Wrapping bean behavior at creation | BeanPostProcessor, HttpServletRequestWrapper |
| Facade | Simplified API over subsystems | JmsTemplate, JdbcTemplate (also template method) |
| Composite | Treating groups uniformly | CompositeHandlerExceptionResolver, CompositeCacheManager |
| Bridge | Decoupling abstraction from impl | JdbcTemplate over DataSource/Dialect |
| Flyweight | Shared immutable instances | Interned MediaType/HttpStatus constants |
See Proxy Pattern & AOP and Front Controller (which uses adapter and decorator internally).
Behavioral patterns
These govern how objects interact and distribute responsibility.
| Pattern | Where Spring uses it | Example class / annotation |
|---|---|---|
| Template Method | Boilerplate-free resource access | JdbcTemplate, RestTemplate, TransactionTemplate |
| Strategy | Interchangeable pluggable behavior | PasswordEncoder, HandlerMethodArgumentResolver, HttpMessageConverter |
| Observer | Decoupled event notification | ApplicationEventPublisher, @EventListener, @TransactionalEventListener |
| Chain of Responsibility | Sequential request processing | Servlet Filter chain, HandlerInterceptor chain, Spring Security FilterChain |
| Command | Encapsulated unit of work | Runnable/Callable via TaskExecutor, @Scheduled tasks |
| Iterator | Traversing result sets / pages | Pageable/Slice, repository Stream results |
| Mediator | Coordinating collaborators | DispatcherServlet orchestrating MVC components |
| State | Lifecycle phase handling | Bean lifecycle (InitializingBean, SmartLifecycle) |
| Visitor | Operating over a structure | BeanDefinitionVisitor, TypeFilter |
| Interpreter | Evaluating an expression grammar | Spring Expression Language (SpEL) |
See Template Method, Strategy, and Observer & Events.
At-a-glance: the headline mappings
If you remember nothing else, remember these five — they account for the bulk of day-to-day Spring:
| You write… | …and Spring applies | Covered in |
|---|---|---|
Constructor-injected @Service | Dependency Injection (IoC) | DI & IoC |
@Transactional / @Cacheable | Proxy | Proxy & AOP |
jdbc.query(sql, mapper) | Template Method | Template Method |
List<Strategy> injection | Strategy | Strategy |
publishEvent(...) + @EventListener | Observer | Observer |
Note: Many Spring classes embody more than one pattern at once.
JdbcTemplate, for instance, is a template method (it owns the algorithm skeleton) and a facade (it simplifies the JDBC subsystem). Patterns are lenses, not boxes — a single class can be viewed through several.
Tip: When reading Spring source, the class name is often the clearest hint: a
*Templateis template-method, a*Builderis builder, a*Factory/*FactoryBeanis a factory, a*Adapteris adapter, and a*Resolver/*Strategyis almost always strategy.
For the language-level explanation of each pattern with plain-Java examples — and the difference between, say, JDK proxies and a hand-written proxy class — see the companion page below.