Design Patterns used in Spring Framework

In this article, we will discuss the design patterns used in the Spring Framework. Design Patterns denote the best computer programming practices in object-oriented software development. Spring framework has been built by using the following design pattern or standard practices. 

Here is the list of well-known design patterns used in the Spring 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.

To define a bean as a singleton in XML, we would write, for example:
<bean id="accountService" class="com.foo.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
To define a bean as a singleton in java based bean configuration, we would write, for example:
@Configuration
public class AppConfiguration {

    @Bean
    @Scope("singleton") // default scope 
    public UserService userService(){
        return new UserService();
    }
}

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. 

ApplicationContext Example:
package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.close();
    }
}
Read more details about the Factory design pattern here at a Factory design pattern.

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

Model View Controller Pattern


The Model-View-Controller (MVC) software design pattern is a method for separating concerns within a software application. In principle, the application logic, or controller, is separated from the technology used to display information to the user, or the view layer. The model is a communications vehicle between the controller and view layers.

Spring MVC is known to be a lightweight implementation as controllers are POJOs against traditional servlets which makes the testing of controllers very comprehensive. 

A controller returns a logical view name and the view selection with the help of a separate ViewResolver. Therefore, Spring MVC controllers can be used along with different view technologies such as JSP, etc.

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 requests and then pass the requests to corresponding handlers. 
Read more details about Front Controller Pattern here at Front Controller Pattern.

View Helper 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.

Dependency injection or inversion of control (IOC)

The Spring framework has an IOC container that 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 the client code asks for a service (bean) using a 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 the high cost of looking up JNDI for a service, the Service Locator pattern makes use of the caching technique. For the first time, a service is required, Service Locator looks up in JNDI and caches the service object. Further lookup or the 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.

Observer-Observable

This pattern 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.

Conclusion

In this article, we have looked into the design patterns used in the Spring framework.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course