Introduction to Spring Boot – Detailed Notes with Coding Examples
1. Why Spring Boot Exists To understand Spring Boot, we must understand the evolution: Servlet → Spring Framework (Spring MVC) → Spring Boot Servlets were difficult to manage,…
1. Why Spring Boot Exists
To understand Spring Boot, we must understand the evolution:
Servlet → Spring Framework (Spring MVC) → Spring Boot
Servlets were difficult to manage, Spring MVC reduced complexity, and Spring Boot removed most of the configuration overhead and made development faster.
Also Before understanding Spring and Spring Boot, we should also first understand about “Servlet” and “Servlet Container”.
Servlet – Servlet is a Java Class that handles client request, process the request and then return the response.
Servlet Container – Servlet Container manages the Servlets. A Servlet Container is a runtime environment (like Apache Tomcat) that manages the lifecycle of servlets, handles client requests, maps them to the correct servlet, and sends back the response while providing services like threading, security, and session management.
2. Servlet Basics
What is a Servlet?
A servlet is a Java class that:
- Handles client request
- Processes it
- Returns response
Servlet Example
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class DemoServlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("Inside doGet of DemoServlet1");
String path = request.getRequestURI();
if (path.equals("/demoServlet1/first")) {
response.getWriter().write("First Endpoint");
} else if (path.equals("/demoServlet1/second")) {
response.getWriter().write("Second Endpoint");
}
}
}What is Servlet Container?
A servlet container manages servlets. Example: Tomcat
It is responsible for:
- Loading servlets
- Mapping requests
- Executing servlet methods
web.xml Mapping
This file maps URL to servlet.
<web-app>
<servlet>
<servlet-name>DemoServlet1</servlet-name>
<servlet-class>DemoServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet1</servlet-name>
<url-pattern>/demoServlet1/*</url-pattern>
</servlet-mapping>
</web-app>Request Flow in Servlet
- Request comes to Tomcat
- Tomcat checks web.xml
- Finds matching servlet
- Calls doGet/doPost
- Sends response

Problems with Servlets
- web.xml becomes very large
- Hard to manage mappings
- Tight coupling
- Difficult unit testing
- Complex API handling using if-else
3. Spring Framework (Spring MVC)
Spring solves servlet problems.
Annotation-Based Configuration
Instead of web.xml:
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class DemoController {
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}Inversion of Control (IOC) and Dependency Injection
IOC means Spring manages object creation and lifecycle.
Without Dependency Injection (Tight Coupling)
class User {
public String getUserDetails(int id) {
return "User " + id;
}
}
class Payment {
User user = new User(); // tightly coupled
public String getSenderDetails(int id) {
return user.getUserDetails(id);
}
}Problem:
- Cannot mock User class
- Hard to test
With Dependency Injection
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
class User {
public String getUserDetails(int id) {
return "User " + id;
}
}
@Component
class Payment {
@Autowired
private User user;
public String getSenderDetails(int id) {
return user.getUserDetails(id);
}
}Now:
- Spring creates objects
- Injects dependencies
- Easy to mock and test
REST API Handling in Spring MVC
Instead of if-else logic:
@RestController
@RequestMapping("/payment")
public class PaymentController {
@GetMapping("/first")
public String first() {
return "First API";
}
@GetMapping("/second")
public String second() {
return "Second API";
}
}4. Spring MVC Architecture
DispatcherServlet (Front Controller)
All requests go through DispatcherServlet.
Flow with Example
@RestController
@RequestMapping("/payment")
public class PaymentController {
@GetMapping("/pay")
public String pay() {
return "Payment Done";
}
}Flow:
- Request → Tomcat
- Tomcat → DispatcherServlet
- DispatcherServlet → finds controller
- Creates object using IOC
- Calls method
- Returns response
Handler Mapping
Maps URL to controller method
Example:
@RequestMapping("/payment")
@GetMapping("/pay")5. Spring MVC Configuration
pom.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>
</dependency>
</dependencies>Configuration Class
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}DispatcherServlet Configuration
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class DispatcherServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}Problems in Spring MVC
- Manual dependency management
- Too many configuration classes
- Need to deploy WAR file
How Spring Framework Solves the Limitations of Servlets
Removal of web.xml
- this web.xml over the time becomes too big and becomes very difficult to manage and understand.
- Spring framework introduced Annotations based configuration.
Inversion of Control (IoC)
- Servlets depends on Servlet container to create object and maintain its lifecycle.
- IoC is more flexible way to manage object dependencies and its lifecycle (though Dependency injection)
Unit Testing is much harder
- As the object creation depends on Servlet container, mocking is not easy. Which makes Unit testing process harder.
- Spring dependency injection facility makes the Unit testing very easy.
Difficult to manage REST APIs
- Handling different HTTP methods, request parameters, path mapping make code little difficult to understand.
- Spring MVC provides an organised approach to handle the requests and its easy to build RESTful APIs.
6. Spring Boot
Spring Boot simplifies Spring MVC.
Dependency Management
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>No need to define versions for each dependency.
Auto Configuration
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
}This replaces:
- DispatcherServlet config
- Component scan
- MVC setup
Embedded Server
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);
}
}Running this:
- Starts embedded Tomcat
- No WAR deployment needed
7. Spring Boot Controller Example
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/myapi")
public class MyController {
@GetMapping("/first")
public String firstApi() {
return "Hello from Spring Boot";
}
}Flow in Spring Boot
- Run main method
- Embedded Tomcat starts
- Request comes
- DispatcherServlet (internal) handles
- Controller method executes
8. Final Comparison
Servlet:
- XML configuration
- Manual mapping
- Hard to scale
Spring MVC:
- Annotation-based
- IOC and DI
- Still configuration heavy
Spring Boot:
- Auto configuration
- Embedded server
- Minimal setup
9. Key Concepts Summary
Servlet:
- Foundation of web apps
Spring MVC:
- Introduced IOC, DI, annotations
Spring Boot:
- Simplifies everything
- Removes configuration
- Speeds up development