Spring Boot

Boot vs Spring

15 min

Boot vs Spring

Tutorial 06: Spring Boot vs Spring Framework ⚖️

📋 Table of Contents

  1. Understanding the Question
  2. Solution Approach
  3. Prerequisites & Requirements
  4. Key Topics & Plan of Action
  5. Complete Implementation
  6. Important Considerations
  7. Visual Representations
  8. Practice Questions

1. Understanding the Question ❓

What are we trying to achieve?

We need to understand:

  • What is the difference between Spring Boot and Spring Framework?
  • Why was Spring Boot created? - Evolution from Spring Framework
  • When to use Spring Boot vs Spring Framework? - Decision criteria
  • How do they relate to each other? - Relationship and dependencies
  • What are the trade-offs? - Pros and cons of each

The Problem It Solves

Confusion Point:

text
❌ "Are Spring Boot and Spring Framework the same?"
❌ "Can I use Spring Boot without Spring Framework?"
❌ "Is Spring Framework obsolete now?"
❌ "Which one should I learn?"
❌ "Do I need both?"

Clarity:

text
✅ Spring Framework is the foundation
✅ Spring Boot builds on top of Spring Framework
✅ Spring Boot simplifies Spring Framework
✅ Spring Boot != Replacement, it's enhancement
✅ Learn both, use Spring Boot for productivity

Real-World Context

Companies using Spring Framework directly:

  • Legacy applications (pre-2014)
  • Highly customized enterprise systems
  • Applications requiring fine-grained control

Companies using Spring Boot:

  • Netflix, Uber, Amazon (microservices)
  • Startups (rapid development)
  • Modern cloud-native applications

2. Solution Approach 🎯

Definition

Spring Framework:

  • Core framework for enterprise Java applications
  • Provides: Dependency Injection, AOP, Data Access, Web MVC
  • Requires: Manual configuration
  • Created: 2003

Spring Boot:

  • Extension of Spring Framework
  • Provides: Auto-configuration, embedded servers, starter dependencies
  • Requires: Minimal configuration
  • Created: 2014

Core Philosophy

text
┌────────────────────────────────────────────────┐
│   Relationship: Spring Boot vs Spring         │
├────────────────────────────────────────────────┤
│                                                │
│     Spring Framework (Foundation)              │
│     ┌──────────────────────────┐              │
│     │ Core Container (IoC/DI)  │              │
│     │ AOP, Aspects             │              │
│     │ Data Access (JDBC, ORM)  │              │
│     │ Web (MVC, WebFlux)       │              │
│     │ Integration              │              │
│     └──────────┬───────────────┘              │
│                │                               │
│                │ builds on                     │
│                ▼                               │
│     ┌──────────────────────────┐              │
│     │    Spring Boot           │              │
│     │ + Auto-configuration     │              │
│     │ + Embedded servers       │              │
│     │ + Starter dependencies   │              │
│     │ + Actuator               │              │
│     │ + Production features    │              │
│     └──────────────────────────┘              │
│                                                │
└────────────────────────────────────────────────┘

Key Differences

  1. Configuration: XML/Java config vs Auto-configuration
  2. Dependency Management: Manual vs Starter POMs
  3. Server: External vs Embedded
  4. Production: Manual setup vs Actuator
  5. Development Speed: Slower vs Faster

3. Prerequisites & Requirements 📦

Software Requirements

ComponentVersionPurpose
JDK17+Java Development
Maven3.6+Build tool
IDEAnyIntelliJ IDEA recommended

Knowledge Requirements

  • Required: Java basics
  • Required: Understanding of web applications
  • ⚠️ Helpful: XML configuration
  • ⚠️ Helpful: Servlet containers

4. Key Topics & Plan of Action 📚

Key Topics Covered

A. Configuration Comparison

AspectSpring FrameworkSpring Boot
Configuration StyleXML/JavaJava (minimal)
Lines of Config200-5000-20
Setup TimeHoursMinutes
DependenciesManualAutomated
Server ConfigExternalEmbedded

B. Feature Comparison

text
Spring Framework Provides:
├── Dependency Injection (IoC)
├── Aspect-Oriented Programming
├── Data Access (JDBC, ORM, Transactions)
├── Web MVC
├── Security
└── Integration

Spring Boot Adds:
├── Auto-configuration
├── Starter dependencies
├── Embedded servers (Tomcat, Jetty, Undertow)
├── Actuator (metrics, health checks)
├── DevTools (hot reload)
└── CLI

Plan of Action

text
Step 1: Create same app with Spring Framework
Step 2: Create same app with Spring Boot
Step 3: Compare configurations
Step 4: Compare project structures
Step 5: Compare deployment
Step 6: Analyze differences

5. Complete Implementation 💻

Example 1: REST API with Spring Framework

Project Structure (Spring Framework)

text
spring-framework-app/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/
│   │   │       ├── config/
│   │   │       │   ├── AppConfig.java
│   │   │       │   └── WebConfig.java
│   │   │       ├── controller/
│   │   │       │   └── UserController.java
│   │   │       └── model/
│   │   │           └── User.java
│   │   ├── resources/
│   │   └── webapp/
│   │       └── WEB-INF/
│   │           ├── web.xml
│   │           └── dispatcher-servlet.xml
└── pom.xml

pom.xml (Spring Framework - Manual Dependencies)

xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-framework-app</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <properties>
        <spring.version>6.1.0</spring.version>
        <servlet.version>6.0.0</servlet.version>
        <jackson.version>2.15.0</jackson.version>
    </properties>

    <dependencies>
        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring Context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring Web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring Web MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Jackson for JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- Servlet API -->
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

web.xml (Spring Framework - Required)

xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
         https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0">

    <display-name>Spring Framework Application</display-name>

    <!-- Spring Context Configuration -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <!-- Spring Context Loader -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!-- Dispatcher Servlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

dispatcher-servlet.xml (Spring Framework Configuration)

xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Enable component scanning -->
    <context:component-scan base-package="com.example"/>

    <!-- Enable annotation-driven MVC -->
    <mvc:annotation-driven/>

    <!-- Configure message converters for JSON -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
            </list>
        </property>
    </bean>
</beans>

WebConfig.java (Java-based Configuration Alternative)

java
package com.example.config;

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 {
    // Additional MVC configuration can go here
}

UserController.java (Spring Framework)

java
package com.example.controller;

import com.example.model.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private List<User> users = new ArrayList<>();

    @GetMapping
    public ResponseEntity<List<User>> getAllUsers() {
        return ResponseEntity.ok(users);
    }

    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        users.add(user);
        return ResponseEntity.status(HttpStatus.CREATED).body(user);
    }
}

Deployment (Spring Framework)

bash
# Build WAR file
mvn clean package

# Deploy to external Tomcat
cp target/spring-framework-app.war /path/to/tomcat/webapps/

# Start Tomcat
./catalina.sh run

# Access application
curl http://localhost:8080/spring-framework-app/api/users

Example 2: Same REST API with Spring Boot

Project Structure (Spring Boot)

text
spring-boot-app/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/
│   │   │       ├── SpringBootAppApplication.java
│   │   │       ├── controller/
│   │   │       │   └── UserController.java
│   │   │       └── model/
│   │   │           └── User.java
│   │   └── resources/
│   │       └── application.properties
└── pom.xml

pom.xml (Spring Boot - Simplified)

xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>

    <!-- Spring Boot Parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>spring-boot-app</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <!-- Single starter brings everything! -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- No version needed - managed by parent -->
            <!-- Includes: Spring MVC, Tomcat, Jackson, etc. -->
        </dependency>
    </dependencies>

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

SpringBootAppApplication.java (Main Class)

java
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication  // That's it! No XML, no web.xml
public class SpringBootAppApplication {

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

UserController.java (Same as Spring Framework)

java
package com.example.controller;

import com.example.model.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private List<User> users = new ArrayList<>();

    @GetMapping
    public ResponseEntity<List<User>> getAllUsers() {
        return ResponseEntity.ok(users);
    }

    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        users.add(user);
        return ResponseEntity.status(HttpStatus.CREATED).body(user);
    }
}

application.properties (Optional Configuration)

properties
# Server port (optional, defaults to 8080)
server.port=8080

# Application name
spring.application.name=spring-boot-app

Deployment (Spring Boot)

bash
# Build executable JAR
mvn clean package

# Run directly - no external server needed!
java -jar target/spring-boot-app-1.0.0.jar

# Or use Maven plugin
mvn spring-boot:run

# Access application
curl http://localhost:8080/api/users

Example 3: Side-by-Side Comparison

Configuration Files Comparison

FileSpring FrameworkSpring Boot
pom.xml100+ lines, manual versions30 lines, managed versions
web.xml50+ linesNOT NEEDED
dispatcher-servlet.xml30+ linesNOT NEEDED
applicationContext.xml50+ linesNOT NEEDED
application.propertiesN/A5 lines (optional)
Java Config50+ linesAUTO-CONFIGURED
TOTAL CONFIG300+ lines0-20 lines

Dependency Count

bash
# Spring Framework - Manual dependencies
mvn dependency:tree
# Output: 25 dependencies, all explicitly defined

# Spring Boot - Starter dependencies
mvn dependency:tree
# Output: 30+ dependencies, only 1 explicitly defined

Startup Time

text
Spring Framework (on Tomcat):
- Tomcat startup: 3-5 seconds
- Application deployment: 5-10 seconds
- Total: 8-15 seconds

Spring Boot (embedded Tomcat):
- Application startup: 2-4 seconds
- Total: 2-4 seconds

Example 4: Feature-by-Feature Comparison

java
/**
 * COMPREHENSIVE COMPARISON
 */

// ============================================
// 1. DEPENDENCY INJECTION
// ============================================

// SPRING FRAMEWORK
@Configuration
public class AppConfig {
    @Bean
    public UserService userService() {
        return new UserServiceImpl();
    }
}

// SPRING BOOT
// Same! Spring Boot uses Spring Framework's DI

// ============================================
// 2. WEB MVC
// ============================================

// SPRING FRAMEWORK
// Needs: web.xml + dispatcher-servlet.xml + Java config
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    // Manual configuration
}

// SPRING BOOT
// Auto-configured! Just add @RestController

// ============================================
// 3. DATABASE ACCESS
// ============================================

// SPRING FRAMEWORK
@Configuration
public class DataSourceConfig {
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/db");
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

// SPRING BOOT
// Just add properties:
// spring.datasource.url=jdbc:mysql://localhost:3306/db
// spring.datasource.username=root
// spring.datasource.password=password
// DataSource and JdbcTemplate auto-configured!

// ============================================
// 4. TRANSACTION MANAGEMENT
// ============================================

// SPRING FRAMEWORK
@Configuration
@EnableTransactionManagement
public class TransactionConfig {
    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}

// SPRING BOOT
// Auto-configured! Just use @Transactional

// ============================================
// 5. PROPERTY CONFIGURATION
// ============================================

// SPRING FRAMEWORK
@Configuration
@PropertySource("classpath:application.properties")
public class PropertyConfig {
    @Autowired
    private Environment env;

    @Bean
    public PropertySourcesPlaceholderConfigurer propertyConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

// SPRING BOOT
// Auto-configured! Just create application.properties

// ============================================
// 6. LOGGING
// ============================================

// SPRING FRAMEWORK
// Manual setup with log4j.xml or logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- Configuration -->
    </appender>
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

// SPRING BOOT
// Auto-configured Logback
// Just use: logging.level.com.example=DEBUG

// ============================================
// 7. EMBEDDED SERVER
// ============================================

// SPRING FRAMEWORK
// External Tomcat/Jetty required
// Deploy WAR file
// Configure server.xml

// SPRING BOOT
// Embedded Tomcat included
// Run: java -jar app.jar
// Configure: server.port=8080

// ============================================
// 8. PRODUCTION MONITORING
// ============================================

// SPRING FRAMEWORK
// Manual implementation
@RestController
public class HealthController {
    @GetMapping("/health")
    public Map<String, String> health() {
        return Map.of("status", "UP");
    }
}

// SPRING BOOT
// Add spring-boot-starter-actuator
// Get /actuator/health, /actuator/metrics, etc. for free!

6. Important Considerations ⚠️

Best Practices

1. When to Use Spring Framework

java
USE Spring Framework when:
- Working with legacy applications
- Need fine-grained control over every aspect
- Custom integration requirements
- Existing XML configurations to maintain

DON'T use if:
- Starting new project (use Spring Boot)
- Need rapid development
- Building microservices
- Deploying to cloud

📝 WHY: Spring Boot provides better productivity for new projects

2. When to Use Spring Boot

java
USE Spring Boot when:
- Starting new projects
- Building microservices
- Need rapid development
- Deploying to cloud/containers
- Want production-ready features

DON'T use if:
- Need absolute control over every dependency
- Working with legacy XML configurations
- Company standards require pure Spring Framework

📝 WHY: Spring Boot accelerates development without sacrificing power

3. Migration Strategy

java
DO: Migrate gradually
// Step 1: Add Spring Boot parent
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
</parent>

// Step 2: Replace dependencies with starters
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

// Step 3: Remove XML configurations gradually
// Step 4: Add @SpringBootApplication
// Step 5: Test thoroughly

DON'T: Big bang migration
- Don't remove all XML at once
- Don't change everything simultaneously
- Don't skip testing

📝 WHY: Gradual migration reduces risk

Common Pitfalls

Pitfall 1: Mixing Configuration Styles

[!CAUTION] Problem

java
undefined

// Mixing Spring Framework and Spring Boot configurations @Configuration @EnableWebMvc // ❌ Conflicts with Spring Boot auto-configuration public class WebConfig implements WebMvcConfigurer { // ... }

text

> [!SUCCESS] Solution
> ```java
// In Spring Boot, use WebMvcConfigurer without @EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
    // Customize Spring Boot's auto-configuration
}

Explanation:

text
@EnableWebMvc
disables Spring Boot's auto-configuration for MVC.


Pitfall 2: Incorrect Dependency Management

[!CAUTION] Problem

xml
undefined
org.springframework spring-core 5.3.0 ```

[!SUCCESS] Solution

xml
undefined
org.springframework spring-core ```

Pitfall 3: Packaging Issues

[!CAUTION] Problem

text
undefined

Spring Framework: WAR packaging required Spring Boot: JAR vs WAR confusion

text

> [!SUCCESS] Solution
> ```xml
<!-- Spring Boot JAR (preferred) -->
<packaging>jar</packaging>

<!-- Spring Boot WAR (for traditional deployment) -->
<packaging>war</packaging>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

7. Visual Representations 📊

Diagram 1: Architecture Comparison

text
SPRING FRAMEWORK ARCHITECTURE
═══════════════════════════════

Developer Code
┌─────────────────┐
│ XML/Java Config │ (Manual setup)
└────────┬────────┘
┌─────────────────┐
│ Spring Container│
└────────┬────────┘
┌─────────────────┐
│ External Server │ (Tomcat/JBoss)
│  (Deploy WAR)   │
└─────────────────┘


SPRING BOOT ARCHITECTURE
═════════════════════════

Developer Code
┌─────────────────┐
│@SpringBootApp   │ (Auto-config)
└────────┬────────┘
┌─────────────────┐
│ Spring Container│
└────────┬────────┘
┌─────────────────┐
│ Embedded Server │ (Built-in)
│  (Run JAR)      │
└─────────────────┘

Diagram 2: Configuration Comparison

text
SPRING FRAMEWORK CONFIGURATION
═══════════════════════════════

pom.xml (100+ lines)
├── spring-core (5.3.0)
├── spring-web (5.3.0)
├── spring-webmvc (5.3.0)
├── jackson-databind (2.14.0)
└── ... (manual versions)

web.xml (50+ lines)
├── ContextLoaderListener
├── DispatcherServlet
└── ServletMapping

dispatcher-servlet.xml (50+ lines)
├── component-scan
├── annotation-driven
└── message-converters

Total: ~200+ lines of configuration


SPRING BOOT CONFIGURATION
══════════════════════════

pom.xml (30 lines)
├── spring-boot-starter-parent
└── spring-boot-starter-web
    └── (auto includes all needed deps)

application.properties (5 lines)
├── server.port=8080
└── spring.application.name=myapp

@SpringBootApplication (1 annotation)

Total: ~35 lines of configuration

Diagram 3: Development Workflow

text
SPRING FRAMEWORK WORKFLOW
═════════════════════════

1. Setup Project
   ├── Create Maven project
   ├── Add all dependencies manually
   ├── Create web.xml
   ├── Create dispatcher-servlet.xml
   └── Create Java configuration
   Time: 2-4 hours

2. Development
   ├── Write code
   ├── Configure beans manually
   └── Wire dependencies
   Time: Normal

3. Build
   ├── mvn clean package
   └── Produces WAR file
   Time: 30 seconds

4. Deploy
   ├── Install Tomcat
   ├── Copy WAR to webapps
   └── Start Tomcat
   Time: 2-5 minutes

Total Setup: ~3-5 hours


SPRING BOOT WORKFLOW
════════════════════

1. Setup Project
   ├── spring initializr (start.spring.io)
   ├── Select starters
   └── Download project
   Time: 2 minutes

2. Development
   ├── Write code
   └── Auto-configuration handles rest
   Time: Faster

3. Build
   ├── mvn clean package
   └── Produces executable JAR
   Time: 30 seconds

4. Run
   └── java -jar app.jar
   Time: 5 seconds

Total Setup: ~5-10 minutes

Diagram 4: Feature Coverage

text
FEATURE COMPARISON
══════════════════

Core Features (Both Have):
├── ✅ Dependency Injection
├── ✅ AOP
├── ✅ Data Access
├── ✅ Web MVC
├── ✅ Security
├── ✅ Testing
└── ✅ Transaction Management

Spring Boot Additional Features:
├── ✨ Auto-configuration
├── ✨ Starter dependencies
├── ✨ Embedded servers
├── ✨ Actuator (monitoring)
├── ✨ DevTools (hot reload)
├── ✨ CLI
├── ✨ Production-ready defaults
└── ✨ Simplified deployment

Diagram 5: Decision Tree

text
WHEN TO USE WHAT?
═════════════════

Starting New Project?
    ┌───┴───┐
   Yes      No (Legacy)
    │        │
    ▼        ▼
Use Spring  Keep Spring
   Boot     Framework
    │        │
    │        │
    │    Modernizing?
    │        │
    │    ┌───┴───┐
    │   Yes      No
    │    │        │
    │    ▼        ▼
    │  Migrate   Maintain
    │  to Boot   as-is
    │    │
    └────┴────────┐
         Spring Boot Project

8. Practice Questions 📝

Beginner Level

Question 1:

text
Q: What is the main difference between Spring Boot and Spring Framework?
A: Spring Framework is the core framework providing IoC, DI, AOP, etc.
   Spring Boot is built on top of Spring Framework and adds:
   - Auto-configuration
   - Starter dependencies
   - Embedded servers
   - Production-ready features

   Analogy: Spring Framework is the engine, Spring Boot is the complete car
   with automatic transmission, GPS, and all modern features.

Question 2:

text
Q: Can I use Spring Framework without Spring Boot?
A: Yes! Spring Framework existed before Spring Boot (since 2003).
   You can use Spring Framework directly, but you'll need to:
   - Manually configure all beans
   - Manage dependencies and versions
   - Deploy to external server
   - Set up monitoring manually

   It works, but requires more effort.

Question 3:

text
Q: List 5 things Spring Boot does that Spring Framework doesn't.
A:
1. Auto-configuration - Automatic bean setup based on classpath
2. Starter dependencies - Curated dependency bundles
3. Embedded servers - Built-in Tomcat/Jetty/Undertow
4. Actuator - Production monitoring endpoints
5. Simplified deployment - Executable JAR files

Question 4:

text
Q: How many lines of configuration for a simple REST API?

Spring Framework: ~200-300 lines
- pom.xml: 100+ lines
- web.xml: 50+ lines
- dispatcher-servlet.xml: 50+ lines
- Java config: 50+ lines

Spring Boot: ~30-50 lines
- pom.xml: 30 lines
- application.properties: 5-10 lines (optional)
- @SpringBootApplication: 1 annotation

Question 5:

text
Q: Does Spring Boot replace Spring Framework?
A: No! Spring Boot builds ON TOP of Spring Framework.

   Spring Boot = Spring Framework + Productivity Features

   When you use Spring Boot, you're still using Spring Framework.
   Spring Boot just makes it easier to use.

Intermediate Level

Question 6:

text
Q: Convert this Spring Framework configuration to Spring Boot:

Spring Framework pom.xml:
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>6.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>6.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.33</version>
    </dependency>
</dependencies>

A: Spring Boot pom.xml:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Benefits:
- No version management needed
- Starters include all necessary dependencies
- Compatible versions guaranteed

Question 7:

text
Q: What changes when migrating this code from Spring Framework to Spring Boot?

Spring Framework:
@Configuration
@EnableWebMvc
@ComponentScan("com.example")
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

A: Spring Boot:
@SpringBootApplication  // Replaces @Configuration, @EnableWebMvc, @ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

// ViewResolver configured in application.properties:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

// Or keep custom bean if needed:
@Configuration
public class WebConfig {
    @Bean
    public ViewResolver viewResolver() {
        // Custom configuration
    }
}

Key Changes:
- @EnableWebMvc removed (auto-configured)
- ViewResolver can be configured via properties
- Main method added to run embedded server

Question 8:

text
Q: How does embedded server in Spring Boot compare to external Tomcat?

A:
EXTERNAL TOMCAT (Spring Framework):
Pros:
- Centralized management
- One server, multiple apps
- Familiar to ops teams

Cons:
- Separate installation required
- Configuration complexity
- Deployment overhead
- Version conflicts between apps

EMBEDDED SERVER (Spring Boot):
Pros:
- No separate installation
- App is self-contained
- Easy deployment (java -jar)
- No version conflicts
- Perfect for microservices
- Cloud-friendly

Cons:
- Each app has its own server (more memory)
- Can't deploy multiple apps to one server

Use External When:
- Traditional enterprise environment
- Multiple apps on one server
- Centralized ops team

Use Embedded When:
- Microservices
- Cloud deployment
- Containerized apps (Docker/Kubernetes)
- Modern development

Question 9:

text
Q: Explain how auto-configuration works and how it differs from XML config.

A:
XML CONFIGURATION (Spring Framework):
<beans>
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>
</beans>

Characteristics:
- Explicit configuration
- All beans defined manually
- No intelligence
- Changes require XML edits

AUTO-CONFIGURATION (Spring Boot):
# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=password

How it works:
1. Spring Boot detects JDBC driver on classpath
2. Reads datasource properties
3. Auto-creates DataSource bean
4. Auto-creates JdbcTemplate bean
5. All beans ready to use

Characteristics:
- Conditional configuration (@ConditionalOnClass, etc.)
- Intelligent defaults
- Override only what you need
- Properties-based customization

You get the same beans with 90% less configuration!

Question 10:

text
Q: What's the deployment difference?

A:
SPRING FRAMEWORK DEPLOYMENT:
1. Build: mvn clean package → produces WAR file
2. Install Tomcat/JBoss
3. Configure server (server.xml, context.xml)
4. Copy WAR to webapps/
5. Start server
6. Access: http://localhost:8080/myapp/api/users

Files: app.war (10MB) + Tomcat (15MB) = 25MB
Steps: 6 steps
Time: 5-10 minutes

SPRING BOOT DEPLOYMENT:
1. Build: mvn clean package → produces JAR file
2. Run: java -jar app.jar
3. Access: http://localhost:8080/api/users

Files: app.jar (20MB, includes Tomcat)
Steps: 2 steps
Time: 30 seconds

Docker Comparison:
Spring Framework:
FROM tomcat:10
COPY app.war /usr/local/tomcat/webapps/

Spring Boot:
FROM openjdk:17
COPY app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

Advanced Level

Question 11:

text
Q: Design a migration strategy from Spring Framework to Spring Boot.

A: PHASED MIGRATION STRATEGY

Phase 1: Preparation (Week 1)
- Audit current Spring Framework application
- Identify all dependencies
- Document custom configurations
- Set up parallel Spring Boot prototype

Phase 2: Dependency Migration (Week 2)
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
</parent>

Replace:
- spring-webmvc → spring-boot-starter-web
- spring-data-jpa → spring-boot-starter-data-jpa
- spring-security → spring-boot-starter-security

Phase 3: Configuration Migration (Week 3)
Convert XML to Properties:
web.xml → server.port, server.servlet.context-path
dispatcher-servlet.xml → Auto-configured
applicationContext.xml → Java @Configuration or properties

Phase 4: Main Class (Week 4)
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Phase 5: Testing (Week 5)
- Unit tests
- Integration tests
- Performance tests
- UAT

Phase 6: Deployment (Week 6)
- Build executable JAR
- Deploy to test environment
- Monitor and validate
- Production deployment

Risk Mitigation:
- Keep Spring Framework version running
- Use feature flags
- Gradual rollout
- Easy rollback plan

Question 12:

text
Q: When would you NOT use Spring Boot?

A: DON'T USE SPRING BOOT when:

1. Legacy Integration Required:
- Existing Spring Framework app with heavy XML config
- Team expertise only in XML configuration
- Migration cost > benefits

2. Extreme Customization:
- Need absolute control over every library version
- Custom Spring Framework modifications
- Proprietary framework extensions

3. Resource Constraints:
- Embedded server overhead unacceptable
- Need minimal JAR size
- Very constrained environment

4. Company Standards:
- Mandated use of specific versions
- Standardized on Spring Framework
- Compliance requirements

5. Application Server Required:
- WebSphere/WebLogic specific features needed
- Shared libraries across apps required
- Traditional enterprise architecture mandated

Example Scenario:
Company has:
- 50 Spring Framework applications
- Shared JBoss infrastructure
- Ops team trained only on JBoss
- No budget for retraining
- Compliance requirements for specific versions

Decision: Continue with Spring Framework
Reason: Migration cost and risk too high

Question 13:

text
Q: Implement same feature in both frameworks: Database with JPA

A:
SPRING FRAMEWORK IMPLEMENTATION:

pom.xml:
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>6.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.33</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>

JPAConfig.java:
@Configuration
@EnableJpaRepositories(basePackages = "com.example.repository")
@EnableTransactionManagement
public class JPAConfig {

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/db");
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.example.model");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
        em.setJpaProperties(properties);

        return em;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}

TOTAL: ~60 lines of Java configuration + dependencies


SPRING BOOT IMPLEMENTATION:

pom.xml:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

TOTAL: ~5 lines of properties

Everything else auto-configured:
- DataSource
- EntityManagerFactory
- TransactionManager
- JPA repositories

RESULT: 92% less code!

🎯 Key Takeaways

  1. Spring Boot builds on Spring Framework - Not a replacement, an enhancement
  2. 95% less configuration - Auto-configuration eliminates boilerplate
  3. Embedded servers - No external deployment needed
  4. Starter dependencies - Managed, compatible versions
  5. Production-ready - Actuator provides monitoring out-of-the-box
  6. Backward compatible - Can gradually migrate from Spring Framework

Quick Reference

text
WHEN TO USE WHAT?

Use Spring Boot:
✅ New projects
✅ Microservices
✅ Cloud deployments
✅ Rapid development
✅ Modern applications

Use Spring Framework:
✅ Legacy applications
✅ Existing XML configs
✅ Specific control needs
✅ Compliance requirements

Migration Path:
Spring Framework → Spring Boot
(Add parent POM → Add starters → Remove XML → Test)

📚 What's Next?

Recommended Learning Path

From this tutorial, you can proceed to:

  • Tutorial 07: Spring Boot vs Spring Framework Deep Dive - Advanced comparisons
  • Tutorial 12: Auto-Configuration Explained - How auto-configuration works
  • Tutorial 14: Spring Boot Starters - Master dependency management

Congratulations! You now understand the difference between Spring Boot and Spring Framework! 🎉

Practice creating the same application in both frameworks to see the difference firsthand.

Press j for next, k for previous