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.

Proxy Design Pattern

Proxy pattern is used heavily in AOP, and remoting.
A good example of 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 

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 

Factory design pattern

This pattern allows the initialization of 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. [org.springframework.context.ApplicationContext]. below are the most commonly used ApplicationContext implementations. FileSystemXmlApplicationContext (need to provide the full path of the XML bean configuration file to the constructor). ClassPathXmlApplicationContext (need to set CLASSPATH of the bean configuration XML file in order to load the metadata of the beans from an XML file). WebXmlApplicationContext (the container loads the XML file within a web application which has metadata of all beans).
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 on 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 Factory design pattern here
 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 
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

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 
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 

Dependency injection or inversion of control (IOC)

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 Dependency injection Pattern here 

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


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 

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 Context Object Pattern here 


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


  1. Very well explained design patterns used in Spring Framework. It is one of the interview question for Experienced proffessionals.

  2. I have read your blog its very attractive and impressive. I like your blog

  3. That's really awesome blog because i found there lot of valuable Information and i am very glad that you share this blog with us

  4. Comment with your Google Account Kissanime watch online guide if you’d like to be able to manage your comments in the future. If you comment anonymously, you won’t be able to edit or delete your comment. Learn more

  5. Comment with your Google Account season 2 of prison school if you’d like to be able to manage your comments in the future. If you comment anonymously, you won’t be able to edit or delete your comment. Learn more

  6. thanks for sharing the pirate bay such a informative article because gogoanimes i found a lot valuable information....

  7. Its great to see this post. I really appreciate your work Cyberpunk V Jacket

  8. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    website development Pakistan


Post a Comment