Monday, June 4, 2018

Spring MVC vs Spring Boot | Differences between Spring MVC and Spring Boot


Spring MVC vs Spring Boot | Differences between Spring MVC and Spring Boot

spring mvc vs spring boot

I have been hearing a lot of these questions lately,
 Spring Framework vs Spring Boot
Spring MVC vs Spring Boot
Why spring boot when we have springs core

and I don't see any complete answer online, let me try to answer this.

First of all comparisons are made if they fall in same categories,In our case there is no comparison at all, let me simplify it by questioning ourselves.

What does spring framework do?
Spring framework lets you develop java j2ee applications in a rapid easy way,Simple right.
But the problem is setting up spring project manually as it involved lot of configurations.

What does spring boot do??
Spring boot aims to wrap all the spring components in a convenient way with no external xml configuration whatsoever. so basically spring boot lets you create a microservice that wraps the spring core in an easy way.

If you create a spring boot project using spring intializr or in any other way, you will see below maven dependency in pom.xml file which downloads all the spring core components that is required to develop an application,you don't need to add manually each component in the project as you do in manual set up of spring framework.


Components like springs core, hibernate validators, logging etc will be downloaded.


<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
</parent>

  
Since above dependency is a parent, all the other dependencies will be treated as child, No need to mention the version of any child dependencies you are going to add, compatible child dependencies are added based on the parent version.

This is a huge advantage of spring boot over manual set up of springs framework, Just imagine manually adding all the dependencies that needs to be compatible to all other existing dependencies, we have all suffered that pain.

Typical spring boot pom would look as shown below

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.spring.security.demo</groupId>
 <artifactId>SpringSecurity</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>SpringSecurity</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.1.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-test</artifactId>
   <scope>test</scope>
  </dependency>
  
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>
  <!-- To compile JSP files -->
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
  </dependency>
  
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>


</project> 
In addition to that, Spring boot provides Spring Boot Data JPA which helps in quering with database in a much easy and quick way, See how it is here


Feature Spring Framework Spring Boot
Configuration XML(lot of pain) Annotations(No pain buddy)
Server Need External Server Comes with Embedded Server
ORM Hibernate Spring Boot Data JPA
Actuator Supported Works well with spring boot
Controller Only Web Web and Rest
Do not consider above table as comparison, try to consider as advantages of spring boot over spring framework.

Final Conclusion 

Spring framework was introduced to make it easy for developers to develop an application in short period of time using spring components that will help in focusing more on business logic rather than DAO layer or Controller.

But it involved many problems like manual setup that involved a lot of configuration,persisting mechanism was not very quick and needed some kind of knowledge on hibernate etc.

And Spring boot was introduced to make it much more easy for developers by removing manual setup,removing configuration and introducing spring boot jpa for persisting mechanism.

VERDICT : Spring Boot acts as a container for spring components(spring framework) with very minimal configuration and with lots of other advantages.

I hope it is clear at this this point,If not, feel free to comment.



Share:

4 comments:

  1. Nice tut. But I just wanna point out that we can still create a REST Services with Spring Frameworks without Spring Boot.

    ReplyDelete
  2. can we use spring boot as spring mvc(means like mvc architecture in jsp response )..?

    ReplyDelete
  3. You can write SpringMVC application with 0 xml configurations. For that matter you dont even need web.xml or any other XML. It has 100% annotation based.

    And do you think running application with default configuration is good practice?

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete