Spring Boot is a powerful tool set for setting up applications quickly. In this article, we are doing an analysis of Spring Boot vs Spring MVC.
Introduction
Spring Boot is a utility for setting up applications quickly, offering an out of the box configuration to build Spring-powered applications. For the beginners, this brings a number of confusions and one of the basic question raised by a beginner is what is the Difference between Spring MVC and Spring Boot?.
This post aims to give an answer to this question. By the end of this post, we will have a basic understanding of below points.
- What is the Spring Framework?
- Overview of MVC Framework.
- What is the Spring Boot?
- Is Spring Boot a replacement for the Spring MVC?
- Analysis of Spring Boot vs Spring MVC.
By the end of this post, we learn that Spring MVC and Spring Boot are part of the Spring ecosystem. They are not competing but complimenting each other. Spring Boot and Spring MVC are not comparable or mutually exclusive.To understand it, let’s have a look at both Spring MVC and Spring Boot. Let’s see what kind of problems they are trying to solve for us.
1. Spring MVC
Spring MVC is a model view controller (MVC) based web framework under Spring Framework. It’s the original web framework built on the Servlet API. It provides several ready-to-use features for building a web application.
- Clear separation of roles. Each role — controller, validator, command object, form object, model object,
DispatcherServlet
, handler mapping, view resolver, and so on — can be fulfilled by a specialized object. - A powerful and straightforward configuration of both framework and application classes as JavaBeans.
- Customizable binding and validation.
- Customizable handler mapping and view resolution.
- Powerful theme and local resolution features.
2. Spring Boot
Spring Boot is a powerful utility for setting up an application quickly by offering default configurations to build Spring-powered applications. Here is the feature list for Spring Boot.
- One of the best ready to use the micro services platform.
- Use of an intelligent and convention over configuration approach that reduces the startup and configuration phase of your project significantly.
- Powerful and flexible configuration management using the application.properties or yml file.
- Auto-configuration module.
- Spring Boot starters to offer ready-to-use auto-configuration for your application.
- Production ready actuator module.
- It simplifies Spring dependencies by taking the opinionated view.
2.1 Why Spring Boot
One of the natural questions is “Why do we need Spring Boot“? when we have the Spring and Spring MVC Framework. To answer this question, let’s see what steps for setting up Spring MVC application (Without Spring Boot)
- Create a web application using Maven or IDE of our choice.
- Copy standard web application configurations (web.xml configuration for Spring MVC application).
- Tweak above configurations based on our requirements.
- Configure Spring MVC standard beans like
ViewResolver, MessageSource etc.
- Configure Database properties for our application.
- Establish the DB layer and make sure underlying DB connection is in place before we can start using it (
EntityManagerFactory
,TransactionManager
etc.)
This is not the final list. For the enterprise applications, the above list can grow significantly. On a high level, this is how some of the configurations look like
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletCxt) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh();
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(ac);
ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/app/*");
}
}
For traditional web.xml configuration to register and initialize the DispatcherServlet
<web-app>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
These configurations keep on increasing based on the third-party API’s
- Configurations and set up to use JPA or Hibernate.
- Setup configurations for DB.
- Configurations for other API’s
These steps need for initial project setup, however, they add other complexity into the project or application life cycle.
- Each module has its own configurations.
- Each module has its own set of dependencies (third-party dependencies)
- Upgrading application (e.g.
Spring 4.x to 5.x
) will be complicated as we need to make sure that all required dependencies upgraded correctly. - In case dependencies are not working correctly, trying to find out root cause in a large application is very complicated and challenging.
Spring Boot provides a tool set to handle these problems and let us focus on writing the business rule and not on project setup. I highly recommend reading our article on What is Spring Boot for more detail.
2.2 Spring Boot Auto Configuration – an Intelligent System
One of the powerful features of Spring Boot is its ability to automatically configure our application based on the jar dependencies we are adding to our classpath.Spring Boot auto-configuration is an intelligent system which can offer ready to use the application to us based on the configured jars in our classpath. For example, if Spring Boot finds HSQLDB
in out classpath, it will automatically configure an in-memory database for us.
Auto-configuration is an opinionated system which creates defaults for us based on the classpath configuration but, it is intelligent enough to give us the flexibility to override it.Auto-configuration is non-invasive. At any point, you can define your own configuration to replace specific parts of the auto-configuration. For example, if you add your own DataSource
bean, the default embedded database support backs away.
2.3 Spring Boot Starters – an Intelligent System
Spring Boot Starters are a set of convenient dependency descriptors easily included in any level of application. These starters work as a bootstrapping process for the Spring related technologies, we no longer need to worry about the dependencies and they will be automatically managed by Spring Boot Starters.
To understand it, let’s take an example where we need to build a web application using Spring Boot. With Spring Boot we only need to add a spring-boot-starter-web starter in our pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Above entry in pom.xml will make sure that all required dependencies are in the classpath and we are all set to start working on our web application. If you check your IDE, you may have the similar output under the dependencies section.
For more detail, please read Spring Boot Starters
Summary
In this post, we did an analysis of Spring Boot vs Spring MVC. We learned that Spring Boot and Spring MVC are entirely different and compliment each other well. Spring Boot provides a tool set to quickly created production ready Spring applications.