Spring Framework Design Patterns Overview
This article explains how Spring implements various design patterns—including Simple Factory, Factory Method, Singleton, Adapter, Decorator, Proxy, Observer, Strategy, and Template Method—detailing their implementation approaches, underlying principles, and practical code examples within the Spring container.
Spring uses a variety of classic design patterns to achieve loose coupling, extensibility, and modularity in its IoC container. The BeanFactory is an example of the Simple Factory pattern, creating bean instances based on a unique identifier.
The Factory Method pattern is realized through the FactoryBean interface; when a bean implements this interface, getBean() returns the result of getObject() rather than the factory itself, as illustrated by the MyBatis SqlSessionFactoryBean example.
Spring’s default bean scope is singleton, implemented via a double‑checked locking mechanism in the getSingleton() method of AbstractBeanFactory :
public Object getSingleton(String beanName) {
return getSingleton(beanName, true);
}
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory
singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}The Adapter pattern appears in Spring MVC via HandlerAdapter , which bridges DispatcherServlet and specific HandlerMapping implementations to produce a ModelAndView .
Decorator functionality is provided by classes whose names contain Wrapper or Decorator , allowing dynamic addition of responsibilities without subclassing.
Proxying is central to Spring AOP; dynamic proxies are created at runtime to weave aspects into target objects, while static proxies require manual implementation.
Spring’s event mechanism follows the Observer pattern. ApplicationEvent (extending EventObject ) represents events, ApplicationListener defines the listener interface, and ApplicationEventPublisher broadcasts events to registered listeners.
The Strategy pattern is embodied by the Resource interface and its various implementations ( UrlResource , ClassPathResource , FileSystemResource , etc.) that encapsulate different resource‑access algorithms.
Template Method is heavily used, for example in JdbcTemplate , which defines the skeleton of JDBC operations and delegates the variable part to callbacks such as StatementCallback :
public final Object execute(String sql) {
Connection con = null;
Statement stmt = null;
try {
con = getConnection();
stmt = con.createStatement();
return executeWithStatement(stmt, sql);
} finally {
closeStatement(stmt);
releaseConnection(con);
}
}
protected abstract Object executeWithStatement(Statement stmt, String sql);By combining the template method with callbacks, Spring avoids subclassing while still allowing custom behavior.
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.