Design Patterns used in Spring Framework

In this post, let's discuss a different kind of design patterns which are widely used in the Spring Framework. Design Patterns denote the best computer programming practices in the object-oriented software development. Spring framework has been built by using the following design pattern or standard practices.I list few known design patterns used in Spring Framework. You may be interested in Design Patterns used in Hibernate Framework

Proxy Design Pattern

The proxy pattern is used heavily in AOP and remoting.

A good example of a proxy design pattern is org.springframework.aop.framework.ProxyFactoryBean. This factory constructs AOP proxy based on Spring beans. The proxy provides a surrogate or placeholder for another object to control access to it.

Read more details about Proxy Design Pattern here at Proxy Design Pattern.

Singleton Design Pattern

Singleton design pattern ensures that there will exist only the single instance of the object in the memory that could provide services.

In the spring framework, the Singleton is the default scope and the IOC container creates exactly one instance of the object per spring IOC container. Spring container will store this single instance in a cache of singleton beans, and all following requests and references for that named bean will get the cached object as return bean. It is recommended to use the singleton scope for stateless beans. We can set up the bean scope as Singleton or prototype (which creates a new bean object for every new request) in the configuration XML file as shown below.
<!-- A bean definition with singleton scope -->
<bean id = "..." class = "..." scope = "singleton/prototype">
   <!-- collaborators and configuration for this bean go here -->
Read more details about Singleton Design Pattern here at Singleton Design Pattern.

Factory design pattern

This pattern allows the initialization of an object through a public static method, called the factory method.

The Spring framework uses the factory design pattern for the creation of the object of beans by using the following two approaches.

Spring BeanFactory Container: – It is the simplest container present in the spring framework which provides the basic support for DI (Dependency Injection). We use the following interface to work with this container. [org.springframework.beans.factory.BeanFactory].

Spring ApplicationContext Container: – It is another container present in spring container which adds extra enterprise-specific functionality. These functionalities include the capability to resolve textual messages from a properties file and publishing application events to the attentive event listeners. We use the following interface to work with this container. 

Examples :
package com.eduonix.springframework.applicationcontext;

import org.springframework.context.ApplicationContext;

public class App {
     public static void main(String[] args) {
 ApplicationContext context = new FileSystemXmlApplicationContext(
 "C:/work/IOC Containers/springframework.applicationcontext/src/main/resources/bean-factory-config.xml");
 HelloApplicationContext obj = (HelloApplicationContext) context.getBean("helloApplicationContext");
 To better understand the case, let's take a look at a real example. The configuration first:
<bean id="welcomerBean" class="com.mysite.Welcomer" factory-method="createWelcomer">
    <constructor-arg ref="messagesLocator">
<bean id="messagesLocator" class="com.mysite.MessageLocator">
    <property name="messages" value="">
And now the bean concerned by this initialization:
public class Welcomer {
  private String message;
  public Welcomer(String message) {
    this.message = message;
  public static Welcomer createWelcomer(MessageLocator messagesLocator) {
    Calendar cal = Calendar.getInstance();
    String msgKey = "";
    if (cal.get(Calendar.AM_PM) == Calendar.AM) {
      msgKey = "";
    return new Welcomer(messagesLocator.getMessageByKey(msgKey));
 Read more details about the Factory design pattern here at a Factory design pattern. This pattern used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc..). For example JdbcTemplate, JmsTemplate, JpaTemplate.

Read more details about Template Design Pattern here at Template Design Pattern. The advantage with Spring MVC is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers. One thing to note is that the controller is only required to return a logical view name, and the view selection is left to a separate ViewResolver. This makes it easier to reuse controllers for different view technologies.

Read more details about Model View Controller here at Model View Controller Pattern.

Front Controller Pattern

Spring provides DispatcherServlet to ensure an incoming request gets dispatched to your controllers.

The front controller design pattern is used to provide a centralized request handling mechanism so that all requests will be handled by a single handler. This handler can do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers. 
Read more details about Front Controller Pattern here at Front Controller Pattern. Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views.

View Helper Pattern separates the static view such as JSPs from the processing of the business model data. 

Frameworks like Spring and Struts provide their own tag libraries to encapsulate processing logic in a helper instead of a view such as JSP files.

Read more details about View Helper Pattern here at View Helper Pattern. The Spring framework has an IOC container which is responsible for the creation of the object, wiring the objects together, configuring these objects and handling the entire life cycle of these objects from their creation until they are completely destroyed. The Spring container has the Dependency Injection (DI) which is used to manage the components present in an application. Such objects are known as Spring Beans.

Read more details about the Dependency injection Pattern here at Dependency injection Pattern.

Service Locator Pattern

ServiceLocatorFactoryBean keeps information of all the beans in the context. When client code asks for a service (bean) using name, it simply locates that bean in the context and returns it. Client code does not need to write spring-related code to locate a bean.
The service locator design pattern is used when we want to locate various services using JNDI lookup. Considering high cost of looking up JNDI for a service, Service Locator pattern makes use of caching technique. For the first time, a service is required, Service Locator looks up in JNDI and caches the service object. Further lookup or same service via Service Locator is done in its cache which improves the performance of the application to a great extent.
Read more details about Service Locator Pattern here at Service Locator Pattern.


it is used in ApplicationContext's event mechanism.

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Read more details about Observer Design Pattern here at Observer Design Pattern.

Context Object Pattern

Context object pattern encapsulating system data in a Context Object allows it to be shared with other parts of the application without coupling the application to a specific protocol.
The ApplicationContext is the central interface within a Spring application for providing configuration information to the application.
Read more details about the Context Object Pattern here at Context Object Pattern.


Please mention in a comment, if you know any other design patterns used in Spring Framework.