Spring MVC vs Spring Boot
Most of us know that Web Development is a quite popular area where people want to learn more As a beginner we also struggle which framework we use to start working and in this blog, we will compare the development of a simple web-based application following Model View Controller pattern in Spring MVC framework and Spring Boot.
The first step while developing a web-based project is to set up the dependencies needed ie the jars needed to run the application.
We need to manually hunt for the dependencies needed eg spring-web MVC, logging, hibernate.
We also need to check the versions which need to be added and also see the compatibility of versions eg which logging version is compatible with which spring-web MVC version.
Developers of Spring MVC know the pain of setting up the pom. Let’s see how Spring Boot takes care of this.
It asks you to specify what kind of project you want,
We go to start.spring.io (Spring Initializer)
Specify dependencies eg Web for a Web-based project
Click download project and import the maven project in eclipse.
Automatically pom is generated.
Spring Boot uses the concept of starters that bundles together compatible jars.
Spring-boot-starter-web takes care of all jars which are the need for the typical web-based application. It automatically bundles hibernate validator, logging with spring-web MVC. We don’t get the headache of selecting versions spring-boot-starter parent takes care of this.
Learn More: Top 5 Online Courses for Spring Boot Developers
Setting Up web.xml
In Spring MVC we need to create web.xml and configure dispatcher servlet.
Spring Boot: The most beautiful concept of Spring Boot is autoconfiguration, so Spring Boot uses intelligence it says if you are using web-based dependencies it means that you obviously need a dispatcher servlet, so why will I ask you to specify it. Spring Boot will automatically configure it, no need to specify it, so no need of dispatcher servlet.
Setting Up Configuration File
Spring MVC: We need to specify all our beans, either we specify all beans in XML format manually or use stereotype annotations to make things easy but still we need an XML or java based configuration files ie an external configuration file.
In Spring Boot main class is annotated with
@SpringBootApplication = @Configuration +@ ComponentScan +@EnableAutoConfiguration.
More on this in below blog
So here @Configuration takes care of beans.xml or an external java based configuration file
In our configuration file of Spring MVC we had to take care of component-scan and specify package in which our beans are present which have been annotated with stereotype annotations (@Service, @Component,@Controller, @Repository)
Spring Boot: Again @SpringBootApplication takes care of this, as @SpringBootApplication also has @ComponentScan which automatically takes care of creating all beans specified in package/subpackage in which main class is present.
Spring MVC: We need to specify view resolver, we need to set up a bean of type view resolver where we specify where to search view related files (prefix) and extension of the file (suffix)
Spring Boot: View resolver is automatically set up, again the magic of @EnableAutoConfiguration. Spring Boot uses common sense. It automatically searches for view fils in src/main/resources/template folder.
More on setting up a simple spring boot application in below blog
If we want to keep view related file in the different folder we can change configuration properties in application.properties.
spring.mvc.view.prefix and spring.mvc.view.suffix example spring.mvc.view.prefix: WEB-INF/views/ spring.mvc.view.suffix:jsp
Setting Up Static Resources
Spring MVC: Again we need to setup static resources in configuration file we need to manually specify where our static resources are present ie css/images.
Spring Boot: No need to set up static resources, automatically src/main/resources/static folder.
Writing the logic
Business logic remains same whether we use Spring Boot or Spring MVC.
Running the application
Now we need to run the application.
Spring MVC: We need to setup a web server (tomcat) and then run the application on tomcat.
Spring Boot: Spring Boot uses the embedded tomcat server, when we run the application by running our main class then automatically embedded tomcat server is run and the application starts on the server.
Tabulate the differences
[table id=2 /]
So in short Spring Boot makes it easy to bootstrap (start) the application as compared to Spring MVC in which before writing business logic we needed to do a lot of pre-work. Spring Boot objective hence is fulfilled where it asks you to directly focus on business logic and not the steps to configure.