Design Pattern Summary in the Spring Framework
This article provides a comprehensive overview of the design patterns used in Spring, including Simple Factory, Factory Method, Singleton, Adapter, Decorator, Proxy, Observer, Strategy, and Template Method, with explanations of their implementation, principles, and practical code examples.
Spring leverages a variety of classic design patterns to achieve loose coupling, extensibility, and flexible bean management. The following patterns are commonly found in the framework:
1. Simple Factory (BeanFactory)
Spring's BeanFactory acts as a simple factory that creates bean instances based on a unique identifier. The bean definition is read from XML, registered in a ConcurrentHashMap , and can be customized via BeanFactoryPostProcessor (e.g., PropertyPlaceholderConfigurer ).
2. Factory Method (FactoryBean)
Implementing FactoryBean turns a bean into a factory; when getBean() is called, Spring invokes the bean's getObject() method and returns its result. A typical example is the integration of Spring with MyBatis.
3. Singleton
Spring beans are singleton by default. The singleton lifecycle is managed in AbstractBeanFactory using double‑checked locking and early reference handling. The core method is:
public Object getSingleton(String beanName) { return getSingleton(beanName, true); }and the detailed implementation handles caching, early exposure, and synchronization.
4. Adapter (HandlerAdapter)
In Spring MVC, HandlerAdapter adapts different handler types to a common execution flow, allowing easy extension by adding new handlers and corresponding adapters.
5. Decorator
Spring uses wrapper or decorator classes (named with Wrapper or Decorator ) to add responsibilities to objects dynamically, offering more flexibility than subclassing.
6. Proxy (AOP)
Spring AOP is built on dynamic proxy (and CGLIB) mechanisms. At runtime, a proxy object is created to weave aspects into target beans, supporting both dynamic and static proxy styles.
7. Observer (Event Model)
The event‑driven model follows the Observer pattern. ApplicationEvent (extending EventObject ) represents events, ApplicationListener handles them, and ApplicationEventPublisher publishes events to registered listeners.
8. Strategy (Resource Interface)
Spring's Resource interface defines a strategy for accessing various underlying resources (e.g., UrlResource , ClassPathResource , FileSystemResource ), each providing specific implementations of methods like getInputStream() , exists() , and getURL() .
9. Template Method (JdbcTemplate)
Spring uses the Template Method pattern combined with callbacks. JdbcTemplate defines the skeleton for JDBC operations, while StatementCallback allows custom code to be executed within that skeleton, handling resource acquisition and release automatically.
These patterns collectively enable Spring to provide a flexible, extensible, and maintainable framework for Java applications.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.