Spring Boot Course

In the context of Spring Boot, layered architecture refers to organizing an application into distinct layers, each responsible for a specific set of tasks or responsibilities. This type of architecture helps to achieve separation of concerns, making the system easier to maintain, extend, and test.
Presentation Layer (Web Layer)
- This layer is responsible for handling user input, processing HTTP requests, and rendering views (or returning API responses).
- It often consists of Controllers (using @RestController for REST APIs or @Controller for MVC-style views) and other web-related components.
- In a Spring Boot application, the controllers are the entry point for incoming requests and typically interact with services to process data.
@RestController @RequestMapping("/api/v1") public class TaskController { private final TaskService taskService; public TaskController(TaskService taskService) { this.taskService = taskService; } @GetMapping(value = "/tasks", produces = MediaType.APPLICATION_JSON_VALUE) public List<Task> getAllTasks() { return taskService.getAllTasks(); } }
Service Layer (Business Logic Layer)
- The service layer contains the business logic and service operations of the application.
- Services often interact with repositories to fetch data from the database, apply any business rules, and provide the necessary data to the controller layer.
- This layer is typically represented by @Service annotated classes in Spring Boot.
public interface TaskService { public List<Task> getAllTasks(); } @Service public class TaskServiceImpl implements TaskService { private final TaskRepository taskRepository; public TaskServiceImpl(TaskRepository taskRepository) { this.taskRepository = taskRepository; } @Override public List<Task> getAllTasks() { return taskRepository.findAll(); } }
Data Access Layer (Repository Layer)
- This layer is responsible for interacting with the data source (usually a database).
- Spring Boot typically uses Spring Data JPA or Spring Data MongoDB for repository interfaces.
- The repository layer provides methods to interact with the database without needing to write custom SQL queries, thanks to the automatic implementation by Spring Data.
@Repository public interface TaskRepository extends JpaRepository<Task, UUID> { // Custom query methods can be added if needed }
DTO Layer (Data Transfer Object)
Used for transferring data between layers, especially between the controller and service layers. It can be used to avoid exposing domain entities directly to the outside world.
public record TaskResponse( UUID id, String title, String description, Status status, Priority priority, LocalDateTime dueDate, LocalDateTime createdAt, LocalDateTime updatedAt ) {}
In summary, layered architecture in Spring Boot helps separate different concerns (presentation, business logic, and data access) into distinct layers. This not only makes the code cleaner and easier to maintain but also helps with testing, scalability, and the ability to manage complex applications more effectively.