Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 90

    Clean Architecture

    Clean Architecture (Uncle Bob) puts the business core at the centre and pushes frameworks, databases, and HTTP to the outside.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Clean Architecture (Uncle Bob) puts the business core at the centre and pushes frameworks, databases, and HTTP to the outside. Dependencies point inward: domain knows nothing of Spring. TechLearningPRO Orders survives Boot upgrades and DB swaps when place-order rules live in use cases, not controllers.

    On Boot 3 / Java 21 you still use Spring — but as a detail. Controllers adapt HTTP to commands; repository adapters implement domain ports with JPA; the domain expresses invariants with plain Java.

    The payoff is testability: use cases run with fakes, no @SpringBootTest required. The cost is more types and discipline — worth it when Orders rules grow beyond CRUD.

    This lesson draws the concentric rings before the pragmatic layered Spring flavour in the next lesson.

    Purpose of this lesson

    Structure Orders so domain and use cases sit inside, with Spring/JPA/HTTP as outer adapters and inward-only dependencies.

    Understanding the topic

    Concentric layers — arrows point inward only.

    • Enterprise/Domain: Order, Money, policies — pure Java.
    • Application/Use cases: PlaceOrder, CancelOrder — orchestrate domain.
    • Interface adapters: controllers, presenters, repository implementations, mappers.
    • Frameworks & drivers: Spring, JPA, Kafka clients, cloud SDKs.
    • Rule: outer layers depend on inner abstractions; never the reverse.

    Step-by-step explanation

    1. Extract domain types (Order, Line, Money) with no Spring imports.
    2. Define a PlaceOrderUseCase that depends on ports (e.g. OrderRepository interface in the app layer).
    3. Implement the port with a JPA adapter in the outer ring.
    4. Keep the REST controller as a thin adapter that maps DTOs ↔ commands.

    Informative example

    Inward dependencies for Orders (ASCII + use-case sketch).

    java
    ┌──────────────────────────────────────────────┐
    │ Frameworks & Drivers │
    │ (Spring, JPA, REST, Kafka clients) │
    │ ┌──────────────────────────────────────┐ │
    │ │ Interface Adapters │ │
    │ │ (Controllers, Repository impls) │ │
    │ │ ┌──────────────────────────────┐ │ │
    │ │ │ Application / Use Cases │ │ │
    │ │ │ (PlaceOrderUseCase, ...) │ │ │
    │ │ │ ┌──────────────────────┐ │ │ │
    │ │ │ │ Enterprise / Domain │ │ │ │
    │ │ │ │ Order, Money, Policy │ │ │ │
    │ │ │ └──────────────────────┘ │ │ │
    │ │ └──────────────────────────────┘ │ │
    │ └──────────────────────────────────────┘ │
    └──────────────────────────────────────────────┘
    public interface OrderRepositoryPort {
    Order save(Order order);
    }
    public final class PlaceOrderUseCase {
    private final OrderRepositoryPort orders;
    public PlaceOrderUseCase(OrderRepositoryPort orders) { this.orders = orders; }
    public Order execute(PlaceOrderCommand cmd) {
    Order order = Order.place(cmd.customerId(), cmd.lines());
    return orders.save(order);
    }
    }

    Real-world use

    When TechLearningPRO moved Orders from Boot 2 to Boot 3, controllers full of JPA entities became a rewrite. Teams that had already isolated place-order in a use case swapped jakarta packages in adapters only — domain tests never noticed Spring.

    Security implications

    Architecture boundaries also contain trust boundaries.

    • Authorize in the application layer (or a dedicated policy) — not only in controllers.
    • Never pass raw credentials into domain objects.
    • Validate untrusted input at the adapter edge before constructing commands.

    Best practices

    • Keep domain free of framework annotations.
    • Test use cases with fake ports; reserve Spring tests for adapters.
    • Map entities at the persistence adapter boundary.

    Common mistakes

    • Importing org.springframework into domain entities.
    • Anemic 'use cases' that only call repositories with no invariants.
    • Skipping ports and leaking JPA entities into JSON responses.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat does 'dependencies point inward' mean?+

    Answer

    Outer frameworks depend on inner domain/application abstractions; the domain never depends on Spring or JPA.
    2IntermediateQuestionWhy is Clean Architecture valuable for Boot upgrades?+

    Answer

    Framework churn stays in adapters; business rules and their tests remain stable.
    3IntermediateQuestionWhere should authorization live?+

    Answer

    At least in the application/policy layer so every entry point (HTTP, messaging) cannot bypass it.

    Hands-on exercise

    Lab. Refactor one Orders flow into Domain + PlaceOrderUseCase + JPA adapter + thin controller. Prove the use case unit-tests without starting Spring.

    Summary

    Clean Architecture protects Orders' business core. Next: the layered Spring shape most teams ship day to day.

    Ready to mark this lesson complete?Track your journey across the entire course.