Java Tutorial

Spring Boot

Building production-ready Java apps with Dependency Injection and Microservices

What is Spring Boot?

Spring Boot is the most popular framework for building modern Java applications. It's built on top of the original Spring Framework, but it automates all the boring setup and configuration, allowing you to build a production-ready application (like a REST API or a web service) in just a few minutes.
Its slogan is "Convention over Configuration."
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@RestController
public class MyApi {
    public static void main(String[] args) {
        SpringApplication.run(MyApi.class, args);
    }

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello from Spring Boot!";
    }
}

Key Concept: Dependency Injection (DI)

Wait! How does Spring actually work? It uses Dependency Injection. Instead of you creating objects with new, the Spring "Container" creates them for you and "injects" them where they are needed. This makes your code highly modular and easy to test.
java
@Service
public class UserService {
    // Business logic...
}

@RestController
public class UserController {
    // Spring INJECTS this service automatically!
    private final UserService service;

    public UserController(UserService service) {
        this.service = service;
    }
}

Comparisons: Spring Boot vs. Traditional Spring

FeatureTraditional SpringSpring Boot
LogicManual XML or Java Config.Auto-Configuration.
ServerDeploy to Tomcat/Jetty externally.Embedded Server (Just run the JAR!).
BuildComplex dependencies."Starters" (Curated dependency groups).

Why Use Spring Boot?

  1. Speed: Go from zero to a working API in seconds using the "Spring Initializr."
  2. Microservices: The primary tools for building "Cloud-Native" distributed systems.
  3. Enterprise: Built-in support for Security (Spring Security), Databases (Spring Data), and Messaging (Kafka/RabbitMQ).
  4. Community: Massive documentation and millions of developers worldwide.

Summary

TermDescription
@SpringBootAppThe entry point for the entire application
BeanAn object managed by the Spring Container
InjectionProviding a Bean to another object automatically
RestControllerA class that handles HTTP requests (APIs)
ServiceA class for business logic and "heavy lifting"
RepositoryA class that talks to the Database
Auto-ConfigSpring guessing what you need based on JARs
StarterA group of related dependencies (e.g., web, data-jpa)
JPAJava Persistence API (Database work!)
MVCModel-View-Controller architecture
Maven/GradleStandard build tools used with Spring
Best ForREST APIs, Microservices, Enterprise Backend systems
StandardEstablished and trusted worldwide for 15+ years
SecurityMost robust authentication/authorization in Java
Applicationapplication.properties (The main config file!)
ActuatorHealth checks and monitoring for production
TestingBuilt-in support for JUnit and Mockito
Profilesdev, prod, test (Different settings for different places!)
SafetyDependency Injection prevents "Spaghetti Code"
ModularBuild your app in layers: Controller -> Service -> DB
Reactive"Spring WebFlux" for high-performance non-blocking logic
LombokCommon library used to remove Java "boilerplate"
Cloud"Spring Cloud" for building massive distributed systems
ModernAlways following the latest Java versions (17, 21)