Design Patterns in Spring
Spring is often described as “a giant bag of design patterns.” Almost every feature you use — dependency injection, @Transactional, JdbcTemplate, ApplicationEvent — is a textbook Gang of Four (GoF) pattern applied at framework scale. Understanding this gives you a mental model for why Spring is shaped the way it is, and a vocabulary for designing your own code to fit naturally inside it.
This section looks at those patterns from two angles: how the framework itself uses them internally, and how you apply the same patterns in your own Spring Boot applications. Every page ties the pattern to concrete Spring classes and annotations with real, compilable code.
Why patterns matter in Spring
The GoF book, Design Patterns: Elements of Reusable Object-Oriented Software, catalogued 23 recurring solutions to object-oriented design problems. Spring’s authors did not bolt patterns on as an afterthought — the framework is built out of them. When you write a @Service bean and inject a @Repository, you are using the dependency injection form of Inversion of Control. When @Transactional wraps your method in a database transaction, a proxy is doing the work. When JdbcTemplate runs your query and hands rows to a RowMapper, that is the template method pattern.
Recognising the pattern behind a feature tells you its trade-offs and its pitfalls — for example, why self-invocation breaks @Transactional (it is a proxy), or why singleton beans must be stateless (they are shared).
The three GoF categories
The 23 GoF patterns fall into three families, and Spring leans on all three.
| Category | Concern | Examples in Spring |
|---|---|---|
| Creational | How objects are created | Singleton beans, @Bean factory methods, FactoryBean<T>, BeanFactory |
| Structural | How objects are composed | AOP proxies (@Transactional), adapters (HandlerAdapter), decorators (BeanPostProcessor) |
| Behavioral | How objects interact | Template method (JdbcTemplate), strategy (PasswordEncoder), observer (ApplicationEvent) |
Common patterns mapped to Spring features
This table is the heart of the section — each row is a pattern and the Spring feature that implements it. The dedicated pages expand each row with code.
| GoF pattern | Where Spring uses it | Key class / annotation |
|---|---|---|
| Dependency Injection (IoC) | Wiring beans together | @Autowired, constructor injection |
| Singleton | Default bean scope | @Component, @Scope("singleton") |
| Factory Method | Programmatic bean creation | @Bean, FactoryBean<T> |
| Abstract Factory | The container itself | ApplicationContext, BeanFactory |
| Proxy | Cross-cutting concerns via AOP | @Transactional, @Cacheable, @Async |
| Template Method | Boilerplate-free resource access | JdbcTemplate, RestTemplate, TransactionTemplate |
| Strategy | Pluggable interchangeable behavior | PasswordEncoder, HandlerMethodArgumentResolver |
| Observer | Decoupled event handling | ApplicationEventPublisher, @EventListener |
| Builder | Fluent object construction | Lombok @Builder, UriComponentsBuilder |
| Front Controller | Centralised request dispatch | DispatcherServlet |
| Adapter | Bridging incompatible interfaces | HandlerAdapter, HandlerInterceptor |
| Decorator | Wrapping bean behavior | BeanPostProcessor |
Tip: If you are coming from plain Java, read the language-level treatment of these patterns first at Design Patterns in Java. This section assumes you know the patterns and focuses on how Spring realises them.
In This Section
- Dependency Injection & IoC — the foundational pattern everything else builds on.
- Singleton Pattern — container-managed singletons vs the classic GoF singleton.
- Factory & Abstract Factory —
@Beanmethods,FactoryBean, and the container as factory. - Proxy Pattern & AOP — how
@Transactionaland friends actually work. - Template Method Pattern —
JdbcTemplateand the*Templatefamily. - Strategy Pattern — injecting collections of interchangeable beans.
- Observer Pattern & Events —
ApplicationEventPublisherand@EventListener. - Builder Pattern — Lombok and framework builders.
- Repository, Service & DTO — the enterprise layering patterns.
- Front Controller (MVC) — the
DispatcherServletrequest flow. - GoF Patterns in Spring — Reference — a compact lookup table.