Three-Tier Architecture in Spring Boot Applications

What is Three-Tier Architecture?

Three-tier architecture is a software design pattern that divides an application into three interconnected layers. In a Spring Boot application, these layers are:

  1. Presentation Layer (Controllers): Handles HTTP requests and responses.

  2. Business Logic Layer (Services): Contains the business logic of the application.

  3. Data Access Layer (Repositories): Interacts with the database.

Presentation Layer (Controllers)

  • Handles HTTP requests and responses

  • Maps URLs to specific methods

  • Validates input data

  • Calls appropriate service methods

  • Returns responses to the client

Example:

@RestController
@RequestMapping("/api/users")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    public UserDTO getUser(@PathVariable Long id) {
        return userService.getUserById(id);
    }
}

Business Logic Layer (Services)

  • Contains the core business logic

  • Coordinates between controllers and repositories

  • Performs data processing and validation

  • Manages transactions

Example:

@Service
public class UserServiceImpl implements UserService {
    private final UserRepository userRepository;

    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public UserDTO getUserById(Long id) {
        User user = userRepository.findById(id)
            .orElseThrow(() -> new EntityNotFoundException("User not found"));
        return convertToDTO(user);
    }
}

Data Access Layer (Repositories)

  • Interacts directly with the database

  • Performs CRUD operations

  • Uses JPA or other data access technologies

Example:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    // JpaRepository provides basic CRUD operations
    // Custom query methods can be added here
}

Layer Dependencies

In this architecture: - Controllers depend on Services - Services depend on Repositories - Each layer only interacts with the layer directly below it

This separation of concerns: - Improves maintainability and testability - Allows for easier refactoring and scaling - Promotes code reuse and modularity