Difference Spring Boot and Spring MVC

Difference Spring Boot and Spring MVC

Spring MVC

  • Framework: Spring MVC is a part of the Spring Framework. It follows the Model-View-Controller design pattern and is used to build web applications.
  • Configuration: Requires a significant amount of configuration (XML or Java-based) to set up web applications.
  • Dependencies: You need to manually manage dependencies and include necessary libraries.
  • Focus: Primarily focused on web application development and the separation of concerns in web applications.

Spring Boot

  • Framework: Spring Boot is a framework built on top of the Spring Framework, designed to simplify the setup and development of Spring applications.
  • Configuration: Provides convention over configuration, meaning it automatically configures the application based on the included dependencies, significantly reducing the need for manual setup.
  • Dependencies: Manages dependencies automatically through its “starters,” which are sets of commonly used libraries grouped together.
  • Focus: Focuses on rapid development, ease of use, and provides features like embedded servers, simplified configuration, and production-ready capabilities.

Spring Boot and Spring MVC

Java Examples: Difference Spring Boot and Spring MVC

1. Dependencies (pom.xml):
```xml
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.10</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
```

Configuration (WebConfig.java):
2. Configuration (WebConfig.java):
```java
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example")
public class WebConfig implements WebMvcConfigurer {
}
```

Controller (HomeController.java):
3. Controller (HomeController.java):
```java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("message", "Hello, Spring MVC!");
        return "home"; // returns the view name "home"
    }
}
```

View (home.jsp):
4. View (home.jsp):
```jsp
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>${message}</h1>
</body>
</html>
```

Spring Boot Example

1. Dependencies (pom.xml):
```xml
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.4</version>
    </dependency>
</dependencies>
```

2. Application Class (Application.java):
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
```

3. Controller (HomeController.java):
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @GetMapping("/")
    public String home() {
        return "Hello, Spring Boot!";
    }
}
```

Homepage

Readmore