Spring Boot

Spring Boot is the standard way to build Java backend services. It takes the Spring framework — the dominant dependency-injection and enterprise toolkit in the Java world — and wraps it in auto-configuration and sensible defaults, so a REST service that once needed pages of XML now starts from a single annotated class.

If you work in enterprise backend development, you will meet Spring Boot: it powers a huge share of the world's banking, retail, and internal-platform services, and its patterns (dependency injection, layered architecture, declarative data access) shape how large Java codebases are organized.

TL;DR

Quick Example

A complete REST service — this single file plus dependencies boots an HTTP server:

Core Concepts

Dependency Injection and Beans

Spring manages object creation. Classes annotated @Component (or the specializations @Service, @Repository, @RestController) become beans the container instantiates and injects wherever they're declared as constructor parameters.

Constructor injection (over field @Autowired) is the modern convention: dependencies are explicit, final, and trivially mockable in tests. This is dependency inversion made mechanical.

Starters and Auto-Configuration

A starter pulls in a coherent set of libraries; auto-configuration activates based on what's present:

Any default can be overridden by defining your own bean — auto-configuration always backs off to your code.

Configuration and Profiles

Activate with SPRING_PROFILES_ACTIVE=prod. Bind groups of settings to typed objects with @ConfigurationProperties.

Layered Architecture

The conventional Spring Boot shape:

@Transactional on service methods wraps them in a database transaction — commit on success, rollback on exception.

Data Access with Spring Data JPA

Repositories are interfaces; Spring generates the implementation:

Entities map classes to tables:

⚠️ The classic JPA trap is the N+1 query: iterating entities and touching a lazy relation per row. Use join fetch in a @Query, or an @EntityGraph, when you know you'll need the relation. See Query Optimization.

Best Practices

Validate at the Boundary

Invalid input returns a 400 with field errors before your logic runs. Prefer request/response records (DTOs) over exposing JPA entities directly — entities leak columns and couple your API to your schema.

Handle Errors in One Place

One @RestControllerAdvice gives consistent error bodies across every controller — the same principle as Express's single error middleware.

Ship Actuator, Guard It

/actuator/health feeds load-balancer checks; /actuator/prometheus feeds Prometheus. Expose only what you need and secure the rest.

Test at Two Levels

Slice tests keep feedback fast; Testcontainers verifies real database behavior instead of mocking the repository layer.

Spring Boot vs Alternatives

GraalVM native images and Project Loom virtual threads have answered most of the historical "Java is heavy/blocking" criticisms — modern Spring Boot supports both.

Common Mistakes

Field Injection Instead of Constructor Injection

Exposing Entities as API Responses

Returning @Entity objects serializes every column (and can trigger lazy-loading explosions mid-serialization). Map to DTO records at the controller boundary.

ddl-auto: update in Production

Letting Hibernate mutate the production schema is unpredictable and irreversible. Use versioned migrations (Flyway or Liquibase) and set ddl-auto: validate.

One Giant @SpringBootTest for Everything

Full-context tests are slow. Reserve them for true integration paths; use @WebMvcTest/@DataJpaTest slices for the rest.

FAQ

What's the difference between Spring and Spring Boot?

Spring is the underlying framework (dependency injection, MVC, transactions). Spring Boot is the opinionated layer on top: auto-configuration, embedded servers, starters, and production tooling. New projects always start with Spring Boot.

Is Spring Boot only for huge enterprise apps?

No — a minimal REST service is a few files. It scales up well (which is why enterprises use it), but small services are perfectly idiomatic, and Quarkus-style competitors have pushed Boot's startup and memory footprint down significantly.

Java or Kotlin for Spring Boot?

Both are first-class. Kotlin reduces boilerplate (data classes, null safety) and Spring ships Kotlin-specific extensions; Java records and modern syntax have closed much of the gap. Team familiarity should decide.

How does Spring Boot compare to Node.js for APIs?

Spring Boot brings stronger typing, a mature transaction/data layer, and enterprise integration at the cost of more ceremony and memory. Node (Express) starts faster and shares a language with the frontend. Both scale; ecosystems and team skills matter more than raw framework choice.

What is Spring Security?

The Spring module for authentication and authorization — filter chains, OAuth 2.0/OIDC login, JWT resource servers, and method-level rules (@PreAuthorize). Powerful, and famously worth reading the docs before fighting.

Related Topics

References