What you'll learn
Small programs fit in your head; large applications need architecture — deliberate structure that keeps a big codebase maintainable as teams and features grow. This module surveys the patterns and frameworks that power real-world enterprise Java.
By the end you'll be able to:
- Organise an app into layers with clear responsibilities
- Apply the repository and service patterns, and use DTOs
- Understand dependency injection — the backbone of Spring
- Map objects to tables with an ORM (JPA/Hibernate)
- Build REST APIs with Spring Boot
- Recognise microservices, messaging, and caching concepts
Layered architecture
The most common structure splits an app into layers, each with one job. A request flows down through them, and each layer only talks to the one below it. This separation of concerns means you can change the database without touching business logic, or swap the web framework without rewriting your rules:
Note
Repository & service patterns
The Repository pattern (Module 23's DAO, formalised) hides all data access behind an interface like UserRepository with methods such as findById and save. The Service layer holds business logic and coordinates repositories. This keeps each concern in one place and makes both easy to test.
Dependency injection
Dependency injection (DI) is the idea that a class shouldn't create its own dependencies — it should receive them (usually through its constructor). This is the practical form of the "D" in SOLID (Module 33), and it makes code loosely coupled and trivially testable:
class OrderService {
private final OrderRepository repo; // depend on the INTERFACE
OrderService(OrderRepository repo) { // injected, not created here
this.repo = repo;
}
void place(Order order) {
// business rules here...
repo.save(order);
}
}
// A test can pass in a mock repo; production passes the real one.
// The service doesn't know or care which — that's the power of DI.Key idea
ORM: JPA & Hibernate
Writing JDBC by hand for every query is tedious. An ORM (Object-Relational Mapping) maps Java objects to database tables automatically. JPA is the standard specification; Hibernate is the most popular implementation. You annotate a class as an @Entity, and the ORM generates the SQL to save and load it — turning rows into objects and back.
REST APIs & Spring Boot
Most backends expose a REST API — resources (like /users) manipulated with HTTP methods (GET to read, POST to create, PUT to update, DELETE to remove). Spring Boot is the dominant Java framework for building these: it bundles DI, an embedded web server, data access, and sensible defaults so you can build a production API with remarkably little code:
@RestController
class UserController {
private final UserService service;
UserController(UserService service) { // Spring injects this for you
this.service = service;
}
@GetMapping("/users/{id}")
User getUser(@PathVariable Long id) {
return service.findById(id);
}
}Note
Going distributed
At scale, single applications grow into distributed systems. A few concepts to know:
- Microservices — split a big app into small, independently deployable services, each owning its data
- Messaging / event-driven — services communicate asynchronously via queues (Kafka, RabbitMQ), decoupling them
- Caching — store hot data in fast memory (Redis) to reduce database load
- Configuration & environments — the same build runs in dev, staging, and prod via external config
Recap & quick check
Key takeaways
- Layered architecture (Controller → Service → Repository → DB) separates concerns.
- Repository hides data access; Service holds business logic; DTOs move data across boundaries.
- Dependency injection means classes receive their dependencies — loosely coupled and testable (Spring's core).
- An ORM (JPA/Hibernate) maps @Entity objects to tables, generating SQL for you.
- Spring Boot builds REST APIs with minimal code; microservices, messaging, and caching scale it further.
Quick check
1. In a layered backend, which layer holds business logic?
2. What does dependency injection mean?
3. What does an ORM like Hibernate do?
4. Which HTTP method typically creates a new resource in REST?
5. What is a DTO?
Great — you can now see how large systems fit together. Next up: Module 38 — Performance Optimization.