Skip to content
Spring Boot sb design-patterns 3 min read

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.

CategoryConcernExamples in Spring
CreationalHow objects are createdSingleton beans, @Bean factory methods, FactoryBean<T>, BeanFactory
StructuralHow objects are composedAOP proxies (@Transactional), adapters (HandlerAdapter), decorators (BeanPostProcessor)
BehavioralHow objects interactTemplate 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 patternWhere Spring uses itKey class / annotation
Dependency Injection (IoC)Wiring beans together@Autowired, constructor injection
SingletonDefault bean scope@Component, @Scope("singleton")
Factory MethodProgrammatic bean creation@Bean, FactoryBean<T>
Abstract FactoryThe container itselfApplicationContext, BeanFactory
ProxyCross-cutting concerns via AOP@Transactional, @Cacheable, @Async
Template MethodBoilerplate-free resource accessJdbcTemplate, RestTemplate, TransactionTemplate
StrategyPluggable interchangeable behaviorPasswordEncoder, HandlerMethodArgumentResolver
ObserverDecoupled event handlingApplicationEventPublisher, @EventListener
BuilderFluent object constructionLombok @Builder, UriComponentsBuilder
Front ControllerCentralised request dispatchDispatcherServlet
AdapterBridging incompatible interfacesHandlerAdapter, HandlerInterceptor
DecoratorWrapping bean behaviorBeanPostProcessor

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

Last updated June 13, 2026
Was this helpful?