Spring Boot Layered Architecture

박진석·2025년 2월 3일
0
post-thumbnail

Spring Boot Layered Architecture

Overview

Spring Boot's layered architecture promotes separation of concerns and modularity through distinct layers, each with specific responsibilities.

Layers

1. Presentation Layer

Handles user interactions and HTTP request processing.

Components:

  • Controllers
    • Annotated with @Controller or @RestController
    • Handles HTTP requests
  • DTOs (Data Transfer Objects)
    • Transfers data between presentation and service layers

Note: Should remain thin, focusing only on request handling, validation, and responses

2. Business Logic Layer

Contains the application's core business logic.

Components:

  • Service Classes
    • Annotated with @Service
    • Encapsulates business rules
    • Performs calculations
    • Coordinates between presentation and data access layers
  • Domain Objects
    • Represents core entities and value objects

3. Persistence Layer

Manages database and external data source interactions.

Components:

  • Repositories
    • Interfaces/classes with @Repository annotation
    • Defines CRUD methods
  • Entities
    • Classes representing database tables
    • Uses JPA annotations

Benefits of Layered Architecture

  1. Modularity: Each layer has distinct responsibilities
  2. Separation of Concerns: Clear distinction between layers
  3. Testability: Independent layer testing
  4. Scalability: Components can be scaled separately

ORM (Object-Relational Mapping)

Overview

Enables database handling via Java, bridging the gap between databases and SQL.

Key Components

JPA (Java Persistence API)

  • Standardized database interaction using object-oriented paradigms
  • Allows working with Java objects instead of direct database operations

Hibernate

  • Popular JPA implementation
  • Provides framework for object-to-database mapping

Key Points:
1. JPA is the specification, Hibernate is an implementation
2. Hibernate predates and influenced JPA
3. Hibernate ORM is a mature implementation

Benefits

  1. Abstraction: Focus on ORM code over SQL queries
  2. Portability: Standard interface enables easy switching between implementations
  3. Productivity: Automation reduces development time

Core Concepts

Entities

  • Lightweight Java objects representing persistent data

Entity Manager

  • Manages entity lifecycle

Persistence Context

A logical container managing entity instances between application and database.

Features:
1. Entity Management: First-level cache
2. Identity Management: Ensures unique entity identity
3. Dirty Checking: Automatic change detection
4. Lazy Loading: On-demand entity fetching
5. Write-Behind: Optimized database writes

Spring Data JPA

Overview

Part of Spring Data family, simplifying JPA-based repository implementation.

Repository Interface Example

Traditional Approach (Without Repository)

@Service
public class UserService {
    @PersistenceContext
    private EntityManager entityManager;
    
    public User findById(Long id) {
        return entityManager.find(User.class, id);
    }
}

Modern Approach (With Repository)

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByName(String name);
}

Built-in CRUD Operations

public interface UserRepository extends JpaRepository<User, Long> {
    // Automatic methods:
    // save(Entity)
    // findById(ID)
    // findAll()
    // delete(Entity)
    // count()
    // exists(ID)
}

Service Implementation

@Service
public class UserService {
    @Autowired
    UserRepository userRepository;
    
    // CREATE
    public User createUser(User user) {
        return userRepository.save(user);
    }
    
    // READ
    public Optional<User> findUser(Long id) {
        return userRepository.findById(id);
    }
}

Interface Definition

A contract specifying required method implementations without providing the implementation itself - essentially a blueprint for classes.

0개의 댓글