Clean Architecture
Clean Architecture (Uncle Bob) puts the business core at the centre and pushes frameworks, databases, and HTTP to the outside.
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
- Extract domain types (Order, Line, Money) with no Spring imports.
- Define a
PlaceOrderUseCasethat depends on ports (e.g.OrderRepositoryinterface in the app layer). - Implement the port with a JPA adapter in the outer ring.
- Keep the REST controller as a thin adapter that maps DTOs ↔ commands.
Informative example
Inward dependencies for Orders (ASCII + use-case sketch).
┌──────────────────────────────────────────────┐│ 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.springframeworkinto 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.
1BeginnerQuestionWhat does 'dependencies point inward' mean?+
Answer
2IntermediateQuestionWhy is Clean Architecture valuable for Boot upgrades?+
Answer
3IntermediateQuestionWhere should authorization live?+
Answer
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.