Spring Boot

What is Spring Boot?

15 min

What is Spring Boot?

Tutorial 01: What is Spring Boot and Why Use It? 🚀

📋 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 Spring Boot actually is - Its definition and core purpose
  • Why it exists - The problems it solves
  • When to use it - Appropriate use cases
  • How it differs from traditional approaches - The value proposition

The Problem Spring Boot Solves

Before Spring Boot, creating a Spring application involved:

text
❌ Complex XML configurations (hundreds of lines)
❌ Manual dependency management (version conflicts)
❌ Server deployment setup (Tomcat, JBoss configuration)
❌ Multiple configuration files (web.xml, context.xml, etc.)
❌ Time-consuming project setup (hours to days)
❌ Boilerplate code everywhere

What Spring Boot Delivers

text
✅ Auto-configuration (convention over configuration)
✅ Embedded servers (no external deployment needed)
✅ Starter dependencies (curated dependency sets)
✅ Production-ready features (metrics, health checks)
✅ Minimal configuration (properties files)
✅ Fast development (minutes to create working app)

2. Solution Approach 🎯

Definition

Spring Boot is an opinionated framework built on top of the Spring Framework that simplifies:

  • Application bootstrapping
  • Configuration management
  • Dependency resolution
  • Production deployment

Core Philosophy

text
"Just Run" - Create standalone, production-grade Spring applications with minimal effort

Key Principles

  1. Convention Over Configuration: Sensible defaults reduce configuration
  2. Opinionated Defaults: Pre-configured settings for common scenarios
  3. Standalone Applications: Embedded servers eliminate deployment complexity
  4. Production-Ready: Built-in monitoring and management features

3. Prerequisites & Requirements 📦

Software Requirements

ComponentVersionPurpose
JDK17+Java Development Kit
Maven/Gradle3.6+/7+Build tool
IDEAnyIntelliJ IDEA recommended
Spring Boot3.xFramework version

Knowledge Requirements

  • ✅ Basic Java (Classes, Objects, Interfaces)
  • ✅ Maven/Gradle basics (optional but helpful)
  • ⚠️ Spring Framework (NOT required - Spring Boot teaches this)
  • ⚠️ Web concepts (helpful for web apps)

Installation Steps

bash
# 1. Verify Java installation
java -version
# Should show: openjdk version "17" or higher

# 2. Install Maven (if not installed)
# macOS
brew install maven

# Linux (Ubuntu/Debian)
sudo apt install maven

# Windows - Download from https://maven.apache.org/

# 3. Verify Maven
mvn -version

4. Key Topics & Plan of Action 📚

Key Topics Covered

A. Spring Boot Architecture

  • Spring Boot Starter Parent: Dependency management
  • Auto-Configuration: Automatic bean configuration
  • Embedded Servers: Tomcat, Jetty, Undertow
  • Starters: Pre-packaged dependency sets

B. Spring Boot Components

text
┌─────────────────────────────────────┐
│     Spring Boot Application         │
├─────────────────────────────────────┤
│  @SpringBootApplication             │
│  ├── @EnableAutoConfiguration       │
│  ├── @ComponentScan                 │
│  └── @SpringBootConfiguration       │
└─────────────────────────────────────┘
         ├── Spring Boot Starters
         ├── Auto-Configuration
         ├── Embedded Server
         └── Actuator (Monitoring)

C. Why Use Spring Boot?

Traditional SpringSpring Boot
500+ lines XML config0-10 lines properties
Hours of setupMinutes to production
Manual dependency versionsManaged dependencies
External server requiredEmbedded server
Complex testing setupBuilt-in test support

Plan of Action

text
Step 1: Create a simple "Hello World" application
Step 2: Explore the project structure
Step 3: Understand auto-configuration
Step 4: Add a REST endpoint
Step 5: Run and test the application
Step 6: Compare with traditional Spring

5. Complete Implementation 💻

Example 1: Traditional Spring Application (Before Spring Boot)

Problem: See how complex it was before Spring Boot

xml
<!-- OLD WAY: web.xml (50+ lines) -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    
    <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/spring/dispatcher-config.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>
xml
<!-- OLD WAY: dispatcher-config.xml (100+ lines) -->
<?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">
    
    <context:component-scan base-package="com.example"/>
    <mvc:annotation-driven/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
xml
<!-- OLD WAY: pom.xml with manual dependency versions -->
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.20</version> <!-- Manual version management -->
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.20</version>
    </dependency>
    <!-- ... 20+ more dependencies with versions ... -->
</dependencies>

Example 2: Spring Boot Application (Modern Way)

Project Structure

text
my-springboot-app/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── demo/
│   │   │               ├── DemoApplication.java
│   │   │               └── controller/
│   │   │                   └── HelloController.java
│   │   └── resources/
│   │       └── application.properties
│   └── test/
│       └── java/
│           └── com/
│               └── example/
│                   └── demo/
│                       └── DemoApplicationTests.java
└── pom.xml

Step 1: Create pom.xml

xml
<?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 
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <!-- Spring Boot Parent - Manages all dependency versions -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/>
    </parent>
    
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>17</java.version>
    </properties>
    
    <dependencies>
        <!-- Single starter brings in everything needed for web apps -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- NO VERSION - managed by parent -->
        </dependency>
        
        <!-- Testing support -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <!-- Plugin to create executable JAR -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Step 2: Create Main Application Class

java
package com.example.demo;

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

/**
 * Main entry point for Spring Boot application
 * 
 * @SpringBootApplication is a convenience annotation that combines:
 *   - @Configuration: Marks class as source of bean definitions
 *   - @EnableAutoConfiguration: Enables Spring Boot's auto-configuration
 *   - @ComponentScan: Scans for components in current package and sub-packages
 */
@SpringBootApplication
public class DemoApplication {

    /**
     * Main method - starts the Spring Boot application
     * 
     * @param args Command line arguments
     */
    public static void main(String[] args) {
        // SpringApplication.run() does the magic:
        // 1. Creates ApplicationContext
        // 2. Registers beans
        // 3. Starts embedded server
        // 4. Initializes the application
        SpringApplication.run(DemoApplication.class, args);
    }
}

Step 3: Create REST Controller

java
package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * REST Controller demonstrating Spring Boot's simplicity
 * 
 * @RestController combines @Controller and @ResponseBody
 * Automatically converts return values to JSON
 */
@RestController
public class HelloController {

    /**
     * Simple GET endpoint
     * URL: http://localhost:8080/hello
     */
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot! 🚀";
    }

    /**
     * Endpoint with path variable
     * URL: http://localhost:8080/hello/John
     */
    @GetMapping("/hello/{name}")
    public String helloName(@PathVariable String name) {
        return String.format("Hello, %s! Welcome to Spring Boot!", name);
    }

    /**
     * Endpoint with query parameter
     * URL: http://localhost:8080/greet?name=John&age=25
     */
    @GetMapping("/greet")
    public Map<String, Object> greet(
            @RequestParam(defaultValue = "Guest") String name,
            @RequestParam(required = false) Integer age) {
        
        Map<String, Object> response = new HashMap<>();
        response.put("message", "Hello, " + name + "!");
        response.put("timestamp", LocalDateTime.now());
        
        if (age != null) {
            response.put("age", age);
            response.put("canVote", age >= 18);
        }
        
        return response; // Automatically converted to JSON!
    }

    /**
     * Application info endpoint
     * URL: http://localhost:8080/info
     */
    @GetMapping("/info")
    public Map<String, Object> info() {
        Map<String, Object> info = new HashMap<>();
        info.put("application", "Spring Boot Demo");
        info.put("version", "1.0.0");
        info.put("framework", "Spring Boot 3.2.0");
        info.put("javaVersion", System.getProperty("java.version"));
        info.put("serverTime", LocalDateTime.now());
        
        return info;
    }
}

Step 4: Configuration (Optional)

properties
# src/main/resources/application.properties

# Server Configuration
server.port=8080
server.servlet.context-path=/api

# Application Configuration
spring.application.name=demo-app

# Logging Configuration
logging.level.root=INFO
logging.level.com.example.demo=DEBUG

# JSON formatting
spring.jackson.serialization.indent-output=true

Step 5: Run the Application

bash
# Method 1: Using Maven
mvn spring-boot:run

# Method 2: Build and run JAR
mvn clean package
java -jar target/demo-0.0.1-SNAPSHOT.jar

# Method 3: In IDE
# Right-click DemoApplication.java -> Run

Step 6: Test the Endpoints

bash
# Test basic endpoint
curl http://localhost:8080/hello
# Output: Hello, Spring Boot! 🚀

# Test with path variable
curl http://localhost:8080/hello/Alice
# Output: Hello, Alice! Welcome to Spring Boot!

# Test with query parameters
curl "http://localhost:8080/greet?name=Bob&age=25"
# Output: {
#   "message": "Hello, Bob!",
#   "timestamp": "2024-01-15T10:30:00",
#   "age": 25,
#   "canVote": true
# }

# Test info endpoint
curl http://localhost:8080/info

Example 3: Understanding What Spring Boot Did For Us

java
package com.example.demo.analysis;

/**
 * Behind the scenes: What Spring Boot auto-configured
 */
public class AutoConfigurationExplained {
    
    /*
     * When you run the application, Spring Boot automatically:
     * 
     * 1. EMBEDDED SERVER CONFIGURATION
     *    - Started Tomcat on port 8080
     *    - Configured servlet container
     *    - Set up dispatcher servlet
     * 
     * 2. WEB MVC CONFIGURATION
     *    - Configured Spring MVC
     *    - Set up message converters (Jackson for JSON)
     *    - Configured exception handling
     *    - Set up static resource handling
     * 
     * 3. BEAN CONFIGURATION
     *    - Scanned @RestController
     *    - Registered all @GetMapping methods
     *    - Created necessary beans
     * 
     * 4. DEPENDENCY INJECTION
     *    - Wired up all components
     *    - Managed bean lifecycle
     * 
     * 5. PRODUCTION FEATURES
     *    - Set up error handling
     *    - Configured logging
     *    - Added shutdown hooks
     * 
     * ALL WITHOUT A SINGLE LINE OF XML! 🎉
     */
}

Example 4: Comparison Code Metrics

java
/**
 * CODE COMPARISON METRICS
 * 
 * Traditional Spring MVC Application:
 * ====================================
 * - Configuration files: 5-10 files
 * - Lines of XML: 300-500 lines
 * - Dependencies to manage: 20-30 (with versions)
 * - Setup time: 4-8 hours
 * - Deployment: Separate server required
 * - JAR size: ~30MB (without server)
 * 
 * Spring Boot Application:
 * ========================
 * - Configuration files: 1 file (application.properties)
 * - Lines of XML: 0 lines ✨
 * - Dependencies to manage: 2-3 starters (no versions)
 * - Setup time: 5-10 minutes
 * - Deployment: Self-contained executable JAR
 * - JAR size: ~20MB (includes embedded server)
 * 
 * PRODUCTIVITY GAIN: 95% reduction in setup time!
 */

6. Important Considerations ⚠️

Best Practices

1. Project Structure

java
DO: Follow standard package structure
com.yourcompany.project/
├── ProjectApplication.java     // Main class
├── config/                     // Configuration classes
├── controller/                 // REST controllers
├── service/                    // Business logic
├── repository/                 // Data access
└── model/                      // Domain entities

DON'T: Put everything in one package

2. Main Application Class

java
DO: Keep main class clean
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

DON'T: Add business logic to main class
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        // DON'T add business logic here
        performDatabaseOperations();
        sendEmails();
    }
}

3. Dependency Management

xml
✅ DO: Use Spring Boot starters
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

❌ DON'T: Add individual dependencies
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>6.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>6.0.0</version>
</dependency>
<!-- ... many more ... -->

Common Pitfalls

1. Port Already in Use

bash
# Error: Port 8080 is already in use

# Solution 1: Change port in application.properties
server.port=8081

# Solution 2: Kill process using port 8080
# macOS/Linux
lsof -ti:8080 | xargs kill -9

# Windows
netstat -ano | findstr :8080
taskkill /PID <PID> /F

2. Component Not Found

java
// Problem: Controller not detected
package com.other.package;  // ❌ Outside base package

@RestController
public class MyController { }

// Solution: Put in correct package
package com.example.demo.controller;  // ✅ Under base package

@RestController
public class MyController { }

3. Circular Dependencies

java
// Problem: Circular dependency
@Service
public class ServiceA {
    @Autowired
    private ServiceB serviceB;  // ❌ Depends on ServiceB
}

@Service
public class ServiceB {
    @Autowired
    private ServiceA serviceA;  // ❌ Depends on ServiceA
}

// Solution: Use constructor injection with @Lazy
@Service
public class ServiceA {
    private final ServiceB serviceB;
    
    public ServiceA(@Lazy ServiceB serviceB) {
        this.serviceB = serviceB;
    }
}

Performance Considerations

java
/**
 * STARTUP TIME OPTIMIZATION
 */

// 1. Exclude unnecessary auto-configurations
@SpringBootApplication(exclude = {
    DataSourceAutoConfiguration.class,  // If not using database
    SecurityAutoConfiguration.class     // If not using security
})

// 2. Use lazy initialization (Spring Boot 2.2+)
spring.main.lazy-initialization=true

// 3. Use class-based proxies
spring.aop.proxy-target-class=true

Security Considerations

properties
# Production-ready settings

# 1. Don't expose actuator endpoints in production
management.endpoints.web.exposure.include=health,info

# 2. Use HTTPS in production
server.ssl.enabled=true

# 3. Hide Spring Boot banner
spring.main.banner-mode=off

# 4. Set secure headers
server.servlet.session.cookie.secure=true
server.servlet.session.cookie.http-only=true

7. Visual Representations 📊

Diagram 1: Spring Boot Architecture

text
┌─────────────────────────────────────────────────────────────┐
│                  Spring Boot Application                     │
└─────────────────────────────────────────────────────────────┘
        ┌───────────────────┴───────────────────┐
        │                                       │
        ▼                                       ▼
┌──────────────────┐                  ┌──────────────────┐
│  Spring Boot     │                  │   Spring Boot    │
│    Starters      │                  │ Auto-Configuration│
├──────────────────┤                  ├──────────────────┤
│ - Web            │                  │ Detects:         │
│ - Data JPA       │                  │ - Classpath      │
│ - Security       │                  │ - Beans          │
│ - Test           │                  │ - Properties     │
└────────┬─────────┘                  └────────┬─────────┘
         │                                     │
         └─────────────┬───────────────────────┘
         ┌─────────────────────────┐
         │   Spring Framework      │
         │   (Core Container)      │
         ├─────────────────────────┤
         │ - Dependency Injection  │
         │ - Bean Management       │
         │ - AOP                   │
         └───────────┬─────────────┘
         ┌─────────────────────────┐
         │   Embedded Server       │
         │   (Tomcat/Jetty)        │
         └─────────────────────────┘

Diagram 2: Application Startup Flow

text
┌─────────────────────────────────────────────────────────────┐
│  1. main() method executed                                  │
│     SpringApplication.run(DemoApplication.class, args)      │
└──────────────┬──────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│  2. Spring Boot initializes                                 │
│     - Load configuration                                    │
│     - Scan for @SpringBootApplication                       │
└──────────────┬──────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│  3. Component Scanning                                      │
│     - Scan packages for @Component, @Service, @Repository   │
│     - Register beans in ApplicationContext                  │
└──────────────┬──────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│  4. Auto-Configuration                                      │
│     - Analyze classpath                                     │
│     - Configure beans based on dependencies                 │
│     - Apply conditional configurations                      │
└──────────────┬──────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│  5. Embedded Server Startup                                 │
│     - Initialize Tomcat/Jetty                              │
│     - Configure ports and contexts                          │
│     - Deploy application                                    │
└──────────────┬──────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│  6. Application Ready                                       │
│     - Log startup time                                      │
│     - Application is ready to accept requests              │
└─────────────────────────────────────────────────────────────┘

Diagram 3: Request Processing Flow

text
Client Request
┌──────────────┐
│  Embedded    │
│   Tomcat     │
│  (Port 8080) │
└──────┬───────┘
┌──────────────────┐
│ Dispatcher       │
│ Servlet          │
│ (Routes request) │
└──────┬───────────┘
┌──────────────────┐
│ Handler Mapping  │
│ (Finds @GetMap)  │
└──────┬───────────┘
┌──────────────────┐
│ Controller       │
│ @RestController  │
│ Method execution │
└──────┬───────────┘
┌──────────────────┐
│ Service Layer    │
│ (Business Logic) │
└──────┬───────────┘
┌──────────────────┐
│ Message          │
│ Converter        │
│ (Object→JSON)    │
└──────┬───────────┘
    Response

Diagram 4: Spring Boot vs Traditional Spring

text
TRADITIONAL SPRING                    SPRING BOOT
═════════════════                    ═══════════

Developer writes:                     Developer writes:
┌───────────────┐                    ┌───────────────┐
│ Business Code │                    │ Business Code │
│  Controller   │                    │  Controller   │
│   Service     │                    │   Service     │
│  Repository   │                    │  Repository   │
└───────┬───────┘                    └───────┬───────┘
        │                                    │
        ▼                                    │
┌───────────────┐                            │
│ Configuration │                            │
│  - web.xml    │                            │
│  - context.xml│                            │
│  - pom.xml    │                            │
│  (500+ lines) │                            │
└───────┬───────┘                            │
        │                                    │
        ▼                                    ▼
┌───────────────┐                    ┌───────────────┐
│ Deploy to     │                    │ Auto-Config   │
│ External      │                    │ Embedded      │
│ Tomcat Server │                    │ Server        │
└───────────────┘                    └───────────────┘

TIME: 8 hours                        TIME: 10 minutes
CONFIG: ~500 lines                   CONFIG: ~5 lines

8. Practice Questions 📝

Beginner Level

  1. Q: What does the @SpringBootApplication annotation do? A: It combines three annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan

  2. Q: How do you change the default port in Spring Boot? A: Add

    text
    server.port=9090
    in application.properties

  3. Q: What is the default embedded server in Spring Boot? A: Apache Tomcat

Intermediate Level

  1. Q: Create a REST endpoint that returns the current date and time

    java
    @GetMapping("/current-time")
    public Map<String, Object> getCurrentTime() {
        Map<String, Object> response = new HashMap<>();
        response.put("dateTime", LocalDateTime.now());
        response.put("date", LocalDate.now());
        response.put("time", LocalTime.now());
        return response;
    }
  2. Q: How would you exclude a specific auto-configuration?

    java
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class DemoApplication { }

Advanced Level

  1. Q: Explain the difference between @Component, @Service, @Repository, and @Controller A: All are stereotype annotations. @Component is generic, @Service indicates business logic, @Repository indicates data access layer (adds exception translation), @Controller indicates web controller.

  2. Q: How does Spring Boot decide which beans to create? A: Through auto-configuration: it looks at classpath, existing beans, and property settings to conditionally configure beans.


🎯 Key Takeaways

  1. Spring Boot = Spring Framework + Sensible Defaults + Auto-Configuration
  2. No XML configuration needed - Everything is in Java annotations and properties
  3. Embedded servers eliminate deployment complexity
  4. Starters simplify dependency management
  5. Production-ready features out of the box
  6. Rapid development - From idea to running app in minutes

📚 What's Next?


🔗 References


Congratulations! You now understand what Spring Boot is and why it's revolutionary! 🎉

Practice the code examples and move to the next tutorial when ready.

Press j for next, k for previous