Spring Boot Architecture
A Spring Boot app is layered: the web layer handles HTTP, the service layer owns business rules, the repository layer talks to the database, and configuration wires it together.
Introduction
A Spring Boot app is layered: the web layer handles HTTP, the service layer owns business rules, the repository layer talks to the database, and configuration wires it together. Understanding this layering is the difference between code that scales and code that becomes a 4 000-line god-controller.
Understanding the topic
The four layers of every clean Spring Boot app:
- Controller — translates HTTP ↔ Java. Validates input, returns DTOs. No business logic.
- Service — pure business rules, transactions, orchestration. Talks to repositories, never to HTTP.
- Repository — persistence. Spring Data interface or JdbcClient. No business decisions.
- Domain / Entity — your data model. Records or JPA entities.
Syntax reference
Request flow through a Spring Boot app:
HTTP Request│▼┌──────────────────┐│ DispatcherServlet│ ← Spring's front controller└────────┬─────────┘▼┌──────────────────┐ @RestController│ Controller │ validates DTOs, calls service└── ──────┬─────────┘▼┌──────────────────┐ @Service @Transactional│ Service │ business rules, orchestration└────────┬─────────┘▼┌──────────────────┐ @Repository (Spring Data)│ Repository │ SQL via JPA / JDBC└────────┬─────────┘▼Database
Real-world use
Banks model a transfer as: TransferController (HTTP) → TransferService (idempotency, fraud checks, ledger debit/credit, transactional) → AccountRepository (rows). One responsibility per layer = audits pass and bugs stay local.
Best practices
- Never inject a
Repositoryinto aController. - DTOs at the boundary, entities inside the service. Never leak entities over HTTP.
- Keep controllers thin (<30 lines) — heavy logic = code smell.