91

What design patterns are used in Spring framework?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
  • It includes more than 10 patterns including ..MVC, Front controller, View Helper, Singleton, Prototype, Factory etc . http://www.javabench.in/2012/02/design-patterns-being-used-in-spring.html – Raúl May 28 '14 at 03:19

9 Answers9

77

There are loads of different design patterns used, but there are a few obvious ones:

  • Proxy - used heavily in AOP, and remoting.

  • Singleton - beans defined in spring config files are singletons by default.

  • Template method - used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc..). For example JdbcTemplate, JmsTemplate, JpaTemplate.


Update following comments: For MVC, you might want to read the MVC Reference

Some obvious patterns in use in MVC:

  • Model View Controller :-) . 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.

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

  • View Helper - Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views.

toolkit
  • 49,809
  • 17
  • 109
  • 135
  • Thanks, What are the design patterns used in Spring MVC module? – Isabel Jinson Apr 16 '09 at 10:53
  • 12
    I don't think Spring implements singleton for the beans. Note that a singleton scoped bean will have a single instance **per application context**. So, if your application has two Spring contexts, they load a same singleton bean `Foo` and you retrieve the instance of `Foo` per context, you will get two different object references. Instead, the design pattern applied here is [flyweight](http://en.wikipedia.org/wiki/Flyweight_pattern) – Luiggi Mendoza May 05 '14 at 19:59
  • @Luiggi Mendoza you mean that spring does not have singleton objects? please explain me with some clarification. – Kumaresan Perumal Apr 21 '17 at 15:58
  • 1
    @KumaresanPerumal there's a significal difference between Singleton pattern and singleton instance (note the upper/lowercase S). Singleton pattern is about maintaining a single instance of the object through the whole application. What Spring does when detecting a singleton bean is to create a single instance **per application context**. The application context is the core component of Spring, and serves to delegate creation and retrieval of objects. The singleton bean is created only once per application context. – Luiggi Mendoza Apr 21 '17 at 16:02
  • Links for model view controller,front controller and view helped takes to http://www.oracle.com/technetwork/java/index.html. Could you please update the answer with a working link? – SpringLearner Nov 02 '17 at 12:31
  • Bringing up `JdbcTemplate` and its friends as an example of template method is very common, but I think they have little to do with that pattern (apart from the name). Or else we can call pretty much all cases where you extend a class to override some protected methods an example of the template method pattern. – ddekany Nov 26 '17 at 18:29
  • @toolkit I come across "The Spring Framework codifies formalized design patterns as first-class objects that you can integrate into your own application" this statement while reading spring from docs what does this mean. – legend Sep 28 '18 at 14:31
12

And of course dependency injection, or IoC (inversion of control), which is central to the whole BeanFactory/ApplicationContext stuff.

Chochos
  • 5,155
  • 22
  • 27
10

The DI thing actually is some kind of strategy pattern. Whenever you want to be some logic/implementation exchangeable you typically find an interface and an appropriate setter method on the host class to wire your custom implementation of that interface.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
8

Spring is a collection of best-practise API patterns, you can write up a shopping list of them as long as your arm. The way that the API is designed encourages you (but doesn't force you) to follow these patterns, and half the time you follow them without knowing you are doing so.

skaffman
  • 398,947
  • 96
  • 818
  • 769
3

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.

2

Observer-Observable: it is used in ApplicationContext's event mechanism

ksevindik
  • 59
  • 3
1

Factory pattern is also used for loading beans through BeanFactory and Application context.

Gurpreet
  • 21
  • 1
1

Factory Method patter: BeanFactory for creating instance of an object Singleton : instance type can be singleton for a context Prototype : instance type can be prototype. Builder pattern: you can also define a method in a class who will be responsible for creating complex instance.

0

Spring container generates bean objects depending on the bean scope (singleton, prototype etc..). So this looks like implementing Abstract Factory pattern. In the Spring's internal implementation, I am sure each scope should be tied to specific factory kind class.

jsr
  • 181
  • 1
  • 5