๐Ÿƒ

Spring Boot ์น˜ํŠธ์‹œํŠธ

Spring Boot 4.1.1 & Java 25 ์ตœ์‹  ๊ธฐ์ค€ ํ•ต์‹ฌ ์–ด๋…ธํ…Œ์ด์…˜, Gradle & Maven ๋นŒ๋“œ ์„ค์ •, RESTful API ์„ค๊ณ„, ์˜์กด์„ฑ ์ฃผ์ž…(DI), application.yml ์„ค์ •, JPA ํŠธ๋žœ์žญ์…˜, ์ „์—ญ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฐ ์‹ค๋ฌด ์ตœ์ ํ™” ๋ ˆํผ๋Ÿฐ์Šค

--views
๋‹จ์ˆ˜:
#springboot#spring#spring-boot-4#java#java25#gradle#maven#rest-api#backend#web#spring-security#jpa์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-06

1. ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ & ๊ตฌ์กฐ (Project Setup & Architecture)

Spring Boot๋Š” ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ์ž๋ฐ” ์ƒํƒœ๊ณ„์˜ ๋ณต์žกํ•œ XML ์„ค์ •๊ณผ ๋ณด์ผ๋Ÿฌํ”Œ๋ ˆ์ดํŠธ๋ฅผ ํ˜์‹ ์ ์œผ๋กœ ์ œ๊ฑฐํ•˜๊ณ , ํ”„๋กœ๋•์…˜ ๋ ˆ๋ฒจ์˜ ๋‹จ๋… ์‹คํ–‰ํ˜•(Standalone) ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹ ์†ํ•˜๊ฒŒ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก ์™œ Spring Boot์ธ๊ฐ€? ํ•ต์‹ฌ ๋ฉ˜ํƒˆ ๋ชจ๋ธ (Mental Model)

  1. ์ œ์–ด์˜ ์—ญ์ „(IoC) & ์˜์กด์„ฑ ์ฃผ์ž…(DI): ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ new ํ‚ค์›Œ๋“œ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ˆ˜๋ช…์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋Œ€์‹ , ์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ(ApplicationContext)๊ฐ€ ๊ฐ์ฒด(Bean)์˜ ์ƒ์„ฑ, ์˜์กด์„ฑ ์กฐ๋ฆฝ, ์†Œ๋ฉธ์„ ์ „๋‹ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ณ  ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ์„ ๊ทน๋Œ€ํ™”ํ•ฉ๋‹ˆ๋‹ค.
  2. ์ž๋™ ๊ตฌ์„ฑ (Auto-Configuration): @EnableAutoConfiguration์„ ํ†ตํ•ด ํด๋ž˜์ŠคํŒจ์Šค์— ์กด์žฌํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ (์˜ˆ: mysql-connector-j๊ฐ€ ์žˆ์œผ๋ฉด DataSource ์ž๋™ ๊ตฌ์„ฑ), @ConditionalOnMissingBean ์กฐ๊ฑด๋ถ€ ์• ๋„ˆํ…Œ์ด์…˜์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ปค์Šคํ…€ ๋นˆ์„ ์ •์˜ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ ๊ธฐ๋ณธ ๊ตฌ์„ฑ์„ ์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋‚ด์žฅ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ (Embedded Tomcat): ๋ณ„๋„์˜ ์™ธ์žฅ ํ†ฐ์บฃ ์„œ๋ฒ„ ์„ค์น˜์™€ WAR ๋ฐฐํฌ ์—†์ด, ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ fat JAR์— ์„œ๋ธ”๋ฆฟ ์—”์ง„(Tomcat, Jetty, Undertow)์ด ๋‚ด์žฅ๋˜์–ด ์–ด๋””์„œ๋“  java -jar app.jar ๋ช…๋ น ํ•˜๋‚˜๋กœ ์ฆ‰์‹œ ๊ตฌ๋™๋ฉ๋‹ˆ๋‹ค.
+-----------------------------------------------------------------------------------+
|                        Spring Boot Application Architecture                       |
+-----------------------------------------------------------------------------------+
| [HTTP Request] โ”€โ”€โ–บ Tomcat Thread Pool (Worker Thread)                             |
|                           โ”‚                                                       |
|                           โ–ผ                                                       |
|               [DispatcherServlet (Front Controller)]                              |
|               โ”‚       โ”‚                  โ–ฒ                                        |
|   Handler     โ”‚       โ”‚ HandlerAdapter   โ”‚ View / ResponseEntity                  |
|   Mapping โ—„โ”€โ”€โ”€โ”˜       โ–ผ                  โ”‚                                        |
+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ [Controller Layer (@RestController)] โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+
|                           โ”‚                                                       |
|                           โ–ผ (DI: Constructor Injection)                           |
|                     [Service Layer (@Service)]                                    |
|                     - Business Logic & Declarative Transaction (@Transactional)   |
|                           โ”‚                                                       |
|                           โ–ผ                                                       |
|                     [Repository Layer (@Repository)]                              |
|                     - Spring Data JPA / Hibernate ORM (L1 Cache & Dirty Checking) |
+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+
|                           โ–ผ (HikariCP Connection Pool)                            |
|                     [Database (MySQL / PostgreSQL)]                               |
+-----------------------------------------------------------------------------------+

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux, macOS, Windows 10/11Ubuntu 22.04 LTS, macOS (Apple Silicon), Windows 11๋ชจ๋“  64-bit OS
CPU2 ์ฝ”์–ด ์ด์ƒ4 ์ฝ”์–ด ์ด์ƒSpring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐ๋™ ๋ฐ ํ†ฐ์บฃ ์Šค๋ ˆ๋“œ ํ’€ ์ฒ˜๋ฆฌ
๋ฉ”๋ชจ๋ฆฌ (RAM)2 GB ์ด์ƒ (JVM ํž™ ์ตœ์†Œ 512 MB)4 GB ~ 8 GB ์ด์ƒ๊ฐœ๋ฐœ ํˆด(IntelliJ) ๋ฐ ๋กœ์ปฌ DB ๋ณ‘ํ–‰ ๊ตฌ๋™ ์‹œ
๋””์Šคํฌ (Storage)2 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„10 GB ์ด์ƒ์˜ SSDGradle/Maven ๋กœ์ปฌ ์บ์‹œ ๋ฐ ๋นŒ๋“œ jar ์•„ํ‹ฐํŒฉํŠธ
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„JDK 21 ๋˜๋Š” 25 LTS ํ•„์ˆ˜ (Spring Boot 3.4+ / 4.x ๊ธฐ์ค€)Gradle 8.10+ ๋˜๋Š” Maven 3.9+๊ธฐ๋ณธ ๋‚ด์žฅ ํ†ฐ์บฃ ํฌํŠธ 8080 (TCP)
my-spring-app/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ”‚   โ”œโ”€โ”€ java/com/example/demo/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ DemoApplication.java      โ†’ @SpringBootApplication ์ง„์ž…์ 
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ controller/               โ†’ HTTP ์š”์ฒญ ์ˆ˜์‹  & ์‘๋‹ต ๋ฐ˜ํ™˜
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ service/                  โ†’ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง & ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ repository/               โ†’ DB ์ ‘๊ทผ (Spring Data JPA)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ domain/ (๋˜๋Š” entity/)     โ†’ JPA ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ dto/                      โ†’ ๊ณ„์ธต ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๊ฐ์ฒด (Request/Response)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ config/                   โ†’ ๋ณด์•ˆ, ์›น, ๋น„๋™๊ธฐ ๋“ฑ ๋นˆ ์„ค์ • ํด๋ž˜์Šค
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ exception/                โ†’ ์ปค์Šคํ…€ ์˜ˆ์™ธ & @RestControllerAdvice
โ”‚   โ”‚   โ””โ”€โ”€ resources/
โ”‚   โ”‚       โ”œโ”€โ”€ application.yml           โ†’ ์ „์—ญ ํ™˜๊ฒฝ ์„ค์ • ํŒŒ์ผ
โ”‚   โ”‚       โ”œโ”€โ”€ application-local.yml     โ†’ ๋กœ์ปฌ ๊ฐœ๋ฐœ์šฉ ํ”„๋กœํŒŒ์ผ
โ”‚   โ”‚       โ””โ”€โ”€ static/ & templates/      โ†’ ์ •์  ์ž์› (HTML/CSS)
โ”‚   โ””โ”€โ”€ test/java/com/example/demo/       โ†’ ๋‹จ์œ„/ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ
โ”œโ”€โ”€ build.gradle (๋˜๋Š” pom.xml)            โ†’ ๋นŒ๋“œ ์Šคํฌ๋ฆฝํŠธ ๋ฐ ์˜์กด์„ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
โ””โ”€โ”€ gradlew / gradlew.bat (๋˜๋Š” mvnw)      โ†’ OS๋ณ„ ๋นŒ๋“œ ๋ž˜ํผ ์Šคํฌ๋ฆฝํŠธ
// DemoApplication.java - ์Šคํ”„๋ง ๋ถ€ํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘ ์ง€์ 
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2. Gradle ๋นŒ๋“œ ์„ค์ • (build.gradle - Groovy & Kotlin)

// build.gradle (Groovy DSL) - Spring Boot 4.1.1 & Java 25 ํ‘œ์ค€ ์‹ค๋ฌด ํ…œํ”Œ๋ฆฟ
plugins {
    id 'java'
    id 'org.springframework.boot' version '4.1.1'
    id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25) // Java 25
    }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // 1. ํ•ต์‹ฌ ์Šคํƒ€ํ„ฐ ์˜์กด์„ฑ
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'

    // 2. ๊ฐœ๋ฐœ ํŽธ์˜ & ํ•ซ ๋ฆฌ๋กœ๋“œ
    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    // 3. ๋กฌ๋ณต (Lombok)
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'

    // 4. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋“œ๋ผ์ด๋ฒ„
    runtimeOnly 'com.mysql:mysql-connector-j'
    runtimeOnly 'com.h2database:h2' // ๋กœ์ปฌ/ํ…Œ์ŠคํŠธ ์ธ๋ฉ”๋ชจ๋ฆฌ์šฉ

    // 5. ํ…Œ์ŠคํŠธ ์˜์กด์„ฑ (JUnit 5, AssertJ, Mockito ๋‚ด์žฅ)
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}

// ์‹ค๋ฌด ๋ฐฐํฌ ์ตœ์ ํ™”: ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ fat-jar ์ƒ์„ฑ (plain jar ์ƒ์„ฑ ๋ฐฉ์ง€)
bootJar {
    archiveFileName = 'app.jar'
}
jar {
    enabled = false // plain jar ๋น„ํ™œ์„ฑํ™”
}
# ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” Gradle ๋นŒ๋“œ ๋ช…๋ น์–ด
./gradlew build                # ์ปดํŒŒ์ผ, ํ…Œ์ŠคํŠธ ๋ฐ jar ๋นŒ๋“œ
./gradlew bootRun              # ๋กœ์ปฌ์—์„œ ์Šคํ”„๋ง ๋ถ€ํŠธ ์•ฑ ์ฆ‰์‹œ ์‹คํ–‰
./gradlew clean build -x test  # ํ…Œ์ŠคํŠธ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋น ๋ฅธ ๋นŒ๋“œ
./gradlew dependencies         # ํ”„๋กœ์ ํŠธ ์˜์กด์„ฑ ํŠธ๋ฆฌ ํ™•์ธ

3. Maven ๋นŒ๋“œ ์„ค์ • (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- [1] ์Šคํ”„๋ง ๋ถ€ํŠธ ์Šคํƒ€ํ„ฐ ๋ถ€๋ชจ POM ์ƒ์† (๋ฒ„์ „ ๊ด€๋ฆฌ ๋ฐ ๊ธฐ๋ณธ ํ”Œ๋Ÿฌ๊ทธ์ธ ์ƒ์†) -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.1.1</version>
        <relativePath/> <!-- ๋ถ€๋ชจ POM์„ ์ €์žฅ์†Œ์—์„œ ์กฐํšŒ -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Spring Boot Project</description>

    <!-- [2] Java ๋ฒ„์ „ ๋ฐ ์ธ์ฝ”๋”ฉ ์†์„ฑ ์ •์˜ -->
    <properties>
        <java.version>25</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- [3] ์˜์กด์„ฑ ๋ชฉ๋ก (๋ถ€๋ชจ POM์œผ๋กœ ์ธํ•ด <version> ํƒœ๊ทธ ์ƒ๋žต ๊ฐ€๋Šฅ) -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Lombok & ๊ฐœ๋ฐœ ํˆด -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- DB ๋“œ๋ผ์ด๋ฒ„ -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- ํ…Œ์ŠคํŠธ ์˜์กด์„ฑ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- [4] ๋นŒ๋“œ ๋ฐ ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์ • -->
    <build>
        <plugins>
            <!-- ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ Fat-JAR ํŒจํ‚ค์ง• ํ”Œ๋Ÿฌ๊ทธ์ธ -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
# ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” Maven ๋นŒ๋“œ ๋ช…๋ น์–ด
./mvnw clean package           # ์ปดํŒŒ์ผ, ํ…Œ์ŠคํŠธ ๋ฐ jar ํŒจํ‚ค์ง•
./mvnw spring-boot:run         # ์Šคํ”„๋ง ๋ถ€ํŠธ ์•ฑ ๋กœ์ปฌ ์‹คํ–‰
./mvnw clean package -DskipTests # ํ…Œ์ŠคํŠธ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋น ๋ฅธ ๋นŒ๋“œ
./mvnw dependency:tree         # ์˜์กด์„ฑ ๊ณ„์ธต ๊ตฌ์กฐ ํ™•์ธ

4. ํ•ต์‹ฌ Starter ์˜์กด์„ฑ ํ€ต ๋ ˆํผ๋Ÿฐ์Šค (Starters Cheatsheet)

// ๋ชฉ์ ๋ณ„ ํ•„์ˆ˜ ์Šคํƒ€ํ„ฐ ์•„ํ‹ฐํŒฉํŠธ ์ •๋ฆฌ (Gradle: implementation '...', Maven: <artifactId>...)

// ๐ŸŒ ์›น & API
'org.springframework.boot:spring-boot-starter-web'        // Spring MVC, REST, ๋‚ด์žฅ ํ†ฐ์บฃ(Tomcat)
'org.springframework.boot:spring-boot-starter-webflux'    // ๋…ผ๋ธ”๋กœํ‚น ๋ฆฌ์•กํ‹ฐ๋ธŒ ์›น (Netty ๊ธฐ๋ฐ˜)

// ๐Ÿ—„๏ธ ๋ฐ์ดํ„ฐ & ORM
'org.springframework.boot:spring-boot-starter-data-jpa'   // Spring Data JPA + Hibernate Core
'org.springframework.boot:spring-boot-starter-data-redis' // Redis ์บ์‹œ, ์„ธ์…˜, Lettuce ํด๋ผ์ด์–ธํŠธ
'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' // MyBatis SQL ๋งคํผ ์—ฐ๋™

// ๐Ÿ”’ ๋ณด์•ˆ & ๊ฒ€์ฆ
'org.springframework.boot:spring-boot-starter-security'   // ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์ธ์ฆ & ์ธ๊ฐ€
'org.springframework.boot:spring-boot-starter-validation' // Hibernate Validator (@Valid, @NotNull)

// ๐Ÿ“จ ๋ฉ”์‹œ์ง• & ๋ฐฐ์น˜
'org.springframework.boot:spring-boot-starter-mail'       // JavaMailSender ์ด๋ฉ”์ผ ๋ฐœ์†ก
'org.springframework.boot:spring-boot-starter-batch'      // ๋Œ€์šฉ๋Ÿ‰ ์ฒญํฌ ๋‹จ์œ„ ๋ฐฐ์น˜ ์ž‘์—…

// ๐Ÿ“Š ์šด์˜ & ๊ด€์ธก์„ฑ
'org.springframework.boot:spring-boot-starter-actuator'   // ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒํƒœ(Health), ๋ฉ”ํŠธ๋ฆญ
'io.micrometer:micrometer-registry-prometheus'           // ํ”„๋กœ๋ฉ”ํ…Œ์šฐ์Šค(Prometheus) ๋ชจ๋‹ˆํ„ฐ๋ง ์ˆ˜์ง‘

5. ํ•ต์‹ฌ ์ปดํฌ๋„ŒํŠธ & ์–ด๋…ธํ…Œ์ด์…˜ (Core Annotations & IoC)

// [1] ์Šคํ…Œ๋ ˆ์˜คํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜ (์ž๋™ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ)
@Component    // ๊ธฐ๋ณธ ์Šคํ”„๋ง ๊ด€๋ฆฌ ๋นˆ์œผ๋กœ ๋“ฑ๋ก
@Service      // ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ๊ณ„์ธต (๋ช…์‹œ์  ์—ญํ•  ๊ตฌ๋ถ„)
@Repository   // ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต (ํ”Œ๋žซํผ ์ข…์† ์˜ˆ์™ธ๋ฅผ DataAccessException์œผ๋กœ ๋ณ€ํ™˜)
@Controller   // Spring MVC ์ปจํŠธ๋กค๋Ÿฌ (๋ทฐ ํ…œํ”Œ๋ฆฟ ๋ฐ˜ํ™˜์šฉ)
@RestController // @Controller + @ResponseBody (JSON/REST API ์ „์šฉ)

// [2] ์„ค์ • ํด๋ž˜์Šค ๋ฐ ์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก
@Configuration // ์Šคํ”„๋ง ์„ค์ • ํด๋ž˜์Šค๋กœ ์„ ์–ธ (์‹ฑ๊ธ€ํ†ค CGLIB ํ”„๋ก์‹œ ์ ์šฉ)
public class AppConfig {

    @Bean // ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ๋นˆ์œผ๋กœ ์ˆ˜๋™ ๋“ฑ๋ก
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

// [3] ํ”„๋กœํผํ‹ฐ ๊ฐ’ ์ฃผ์ž…
@Component
public class ApiClient {
    // ๋‹จ์ผ ํ”„๋กœํผํ‹ฐ ๊ฐ’ ์ฃผ์ž… (๊ธฐ๋ณธ๊ฐ’ ์„ค์ • ๊ฐ€๋Šฅ)
    @Value("${external.api.timeout:5000}")
    private int timeout;

    @Value("${external.api.base-url}")
    private String baseUrl;
}

// [4] ํƒ€์ž… ์„ธ์ดํ”„ ํ”„๋กœํผํ‹ฐ ๋ฐ”์ธ๋”ฉ (@ConfigurationProperties)
@Component
@ConfigurationProperties(prefix = "app.mail")
@Getter @Setter
public class MailProperties {
    private String host;
    private int port;
    private List<String> defaultRecipients;
}

6. ์˜์กด์„ฑ ์ฃผ์ž… & ๋นˆ ์ƒ๋ช…์ฃผ๊ธฐ (DI & Bean Lifecycle)

// ์‹ค๋ฌด ๊ถŒ์žฅ ํŒจํ„ด: ์ƒ์„ฑ์ž ์ฃผ์ž… + Lombok @RequiredArgsConstructor
@Service
@RequiredArgsConstructor // final ํ•„๋“œ ๋Œ€์ƒ ์ƒ์„ฑ์ž ์ž๋™ ์ƒ์„ฑ
public class UserService {

    // final ํ‚ค์›Œ๋“œ๋กœ ๋ถˆ๋ณ€์„ฑ ๋ณด์žฅ ๋ฐ ๋ˆ„๋ฝ ๋ฐฉ์ง€
    private final UserRepository userRepository;
    private final EmailService emailService;

    // ๋นˆ ๋“ฑ๋ก ์งํ›„ 1ํšŒ ์ดˆ๊ธฐํ™” ์ž‘์—… ์‹คํ–‰ (DB ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์„ธํŒ… ๋“ฑ)
    @PostConstruct
    public void init() {
        System.out.println("UserService ๋นˆ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ");
    }

    // ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ ์ง์ „ ๋ฆฌ์†Œ์Šค ๋ฐ˜ํ™˜/์ •๋ฆฌ ์ž‘์—… ์‹คํ–‰
    @PreDestroy
    public void cleanup() {
        System.out.println("UserService ๋นˆ ์ข…๋ฃŒ ๋ฆฌ์†Œ์Šค ์ •๋ฆฌ");
    }
}

// ๋™์ผ ํƒ€์ž… ๋นˆ ์ถฉ๋Œ ์‹œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• (@Primary, @Qualifier)
@Component
@Primary // ๊ธฐ๋ณธ ์šฐ์„ ์ˆœ์œ„ ๋นˆ์œผ๋กœ ์ง€์ •
public class KakaoPayService implements PaymentService { }

@Component("naverPay")
public class NaverPayService implements PaymentService { }

@Service
@RequiredArgsConstructor
public class OrderService {
    // ํŠน์ • ๋นˆ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ฃผ์ž…๋ฐ›์„ ๋•Œ
    @Qualifier("naverPay")
    private final PaymentService paymentService;
}

7. RESTful API ์ปจํŠธ๋กค๋Ÿฌ (Spring MVC REST)

@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    // 1. ๋‹จ๊ฑด ์กฐํšŒ: GET /api/v1/users/{id}
    @GetMapping("/{id}")
    public ResponseEntity<UserResponse> getUser(@PathVariable Long id) {
        UserResponse response = userService.findById(id);
        return ResponseEntity.ok(response); // HTTP 200 OK
    }

    // 2. ๋ชฉ๋ก ํŽ˜์ด์ง• ๊ฒ€์ƒ‰: GET /api/v1/users?role=ADMIN&page=0&size=10
    @GetMapping
    public ResponseEntity<Page<UserResponse>> getUsers(
            @RequestParam(required = false, defaultValue = "USER") String role,
            @PageableDefault(size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
        return ResponseEntity.ok(userService.findAll(role, pageable));
    }

    // 3. ์ƒ์„ฑ: POST /api/v1/users
    @PostMapping
    public ResponseEntity<UserResponse> createUser(@Valid @RequestBody UserCreateRequest request) {
        UserResponse created = userService.create(request);
        URI location = URI.create("/api/v1/users/" + created.getId());
        return ResponseEntity.created(location).body(created); // HTTP 201 Created
    }

    // 4. ์ˆ˜์ •: PUT /api/v1/users/{id}
    @PutMapping("/{id}")
    public ResponseEntity<UserResponse> updateUser(
            @PathVariable Long id,
            @Valid @RequestBody UserUpdateRequest request) {
        return ResponseEntity.ok(userService.update(id, request));
    }

    // 5. ์‚ญ์ œ: DELETE /api/v1/users/{id}
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        userService.delete(id);
        return ResponseEntity.noContent().build(); // HTTP 204 No Content
    }
}

8. DTO ์œ ํšจ์„ฑ ๊ฒ€์ฆ (Bean Validation)

// ์˜์กด์„ฑ: implementation 'org.springframework.boot:spring-boot-starter-validation'
@Getter
@NoArgsConstructor
public class UserCreateRequest {

    @NotBlank(message = "์ด๋ฆ„์€ ํ•„์ˆ˜ ์ž…๋ ฅ ํ•ญ๋ชฉ์ž…๋‹ˆ๋‹ค.")
    @Size(min = 2, max = 30, message = "์ด๋ฆ„์€ 2~30์ž ์ด๋‚ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
    private String name;

    @NotBlank(message = "์ด๋ฉ”์ผ์€ ํ•„์ˆ˜ ์ž…๋ ฅ ํ•ญ๋ชฉ์ž…๋‹ˆ๋‹ค.")
    @Email(message = "์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค.")
    private String email;

    @NotBlank(message = "๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ ํ•ญ๋ชฉ์ž…๋‹ˆ๋‹ค.")
    @Pattern(
        regexp = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{8,}$",
        message = "๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 8์ž ์ด์ƒ, ์˜๋ฌธ, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ๊ฐ๊ฐ ์ตœ์†Œ 1๊ฐœ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."
    )
    private String password;

    @Min(value = 14, message = "14์„ธ ์ด์ƒ๋งŒ ๊ฐ€์ž… ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.")
    @Max(value = 120, message = "์œ ํšจํ•œ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
    private int age;
}

9. ์ „์—ญ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ (Global Exception Handling)

// [1] ํ†ต์ผ๋œ ์—๋Ÿฌ ์‘๋‹ต DTO
@Getter
@Builder
public class ErrorResponse {
    private final LocalDateTime timestamp = LocalDateTime.now();
    private final int status;
    private final String error;
    private final String message;
    private final Map<String, String> validationErrors;
}

// [2] ์ „์—ญ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ธฐ (@RestControllerAdvice)
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 1. ์œ ํšจ์„ฑ ๊ฒ€์ฆ ์‹คํŒจ ์˜ˆ์™ธ (@Valid ์‹คํŒจ ์‹œ ์ž๋™ ๋ฐœ์ƒ)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ErrorResponse> handleValidationException(MethodArgumentNotValidException ex) {
        Map<String, String> errors = new HashMap<>();
        ex.getBindingResult().getFieldErrors().forEach(err ->
            errors.put(err.getField(), err.getDefaultMessage())
        );

        ErrorResponse response = ErrorResponse.builder()
                .status(HttpStatus.BAD_REQUEST.value())
                .error(HttpStatus.BAD_REQUEST.getReasonPhrase())
                .message("์ž…๋ ฅ๊ฐ’ ๊ฒ€์ฆ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.")
                .validationErrors(errors)
                .build();
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
    }

    // 2. ๋น„์ฆˆ๋‹ˆ์Šค ์ปค์Šคํ…€ ์˜ˆ์™ธ
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFoundException(ResourceNotFoundException ex) {
        ErrorResponse response = ErrorResponse.builder()
                .status(HttpStatus.NOT_FOUND.value())
                .error(HttpStatus.NOT_FOUND.getReasonPhrase())
                .message(ex.getMessage())
                .build();
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
    }

    // 3. ๊ธฐํƒ€ ์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜ (์ตœํ›„์˜ ๋ณด๋ฃจ)
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleGeneralException(Exception ex) {
        log.error("Unhandled Exception occurred: ", ex);
        ErrorResponse response = ErrorResponse.builder()
                .status(HttpStatus.INTERNAL_SERVER_ERROR.value())
                .error(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase())
                .message("์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.")
                .build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
    }
}

10. application.yml ํ•ต์‹ฌ ์„ค์ • (Configuration)

# ๊ธฐ๋ณธ ์„œ๋ฒ„ ๋ฐ ํ”„๋กœํŒŒ์ผ ์„ค์ •
server:
  port: 8080
  servlet:
    context-path: /
    encoding:
      charset: UTF-8
      force: true

spring:
  application:
    name: lucky-api-service
  profiles:
    active: local # ๊ธฐ๋ณธ ํ™œ์„ฑํ™” ํ”„๋กœํŒŒ์ผ

  # ๋ฐ์ดํ„ฐ์†Œ์Šค & HikariCP ์ปค๋„ฅ์…˜ ํ’€ ์„ค์ •
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/lucky_db?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
    username: root
    password: password123
    hikari:
      maximum-pool-size: 10
      minimum-idle: 5
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000

  # JPA & Hibernate ์„ค์ •
  jpa:
    open-in-view: false # OSIV ๋น„ํ™œ์„ฑํ™” (์ปค๋„ฅ์…˜ ๊ณ ๊ฐˆ ๋ฐฉ์ง€ ๋ฐ ์‹ค๋ฌด ํ•„์ˆ˜ ๊ถŒ์žฅ)
    hibernate:
      ddl-auto: validate # local: update / dev, prod: validate or none
    show-sql: false # sysout ๋Œ€์‹  logback์œผ๋กœ ํ™•์ธ ๊ถŒ์žฅ
    properties:
      hibernate:
        format_sql: true
        default_batch_fetch_size: 100 # N+1 ํ•ด๊ฒฐ์„ ์œ„ํ•œ In-query ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ

# ๋กœ๊น… ๋ ˆ๋ฒจ ์„ค์ •
logging:
  level:
    root: INFO
    com.example.demo: DEBUG
    org.hibernate.SQL: DEBUG # SQL ์ฟผ๋ฆฌ ๋กœ๊น…
    org.hibernate.orm.jdbc.bind: TRACE # ๋ฐ”์ธ๋”ฉ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ํ‘œ์‹œ (Spring Boot 3.x)

11. Spring Data JPA & ํŠธ๋žœ์žญ์…˜ (Data & Transaction)

// Repository ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ (JpaRepository ๊ธฐ๋ณธ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ํ™œ์šฉ)
public interface UserRepository extends JpaRepository<User, Long> {

    // ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ ํŒจํ„ด (์ด๋ฆ„ ๊ธฐ๋ฐ˜ ์ž๋™ ์ฟผ๋ฆฌ ์ƒ์„ฑ)
    Optional<User> findByEmail(String email);
    boolean existsByEmail(String email);
    List<User> findByStatusAndCreatedAtAfter(UserStatus status, LocalDateTime dateTime);

    // JPQL ์ง์ ‘ ์ •์˜ (@Query)
    @Query("SELECT u FROM User u WHERE u.role = :role AND u.isActive = true")
    List<User> findActiveUsersByRole(@Param("role") Role role);

    // ๋ฒŒํฌ ์—…๋ฐ์ดํŠธ/์‚ญ์ œ (@Modifying ํ•„์ˆ˜, ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ ์ดˆ๊ธฐํ™” ์„ค์ •)
    @Modifying(clearAutomatically = true)
    @Query("UPDATE User u SET u.status = :newStatus WHERE u.lastLoginAt < :cutoff")
    int bulkUpdateStatus(@Param("newStatus") UserStatus newStatus, @Param("cutoff") LocalDateTime cutoff);
}

// ์„œ๋น„์Šค ๊ณ„์ธต ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true) // ํด๋ž˜์Šค ์ „์ฒด ์ฝ๊ธฐ ์ „์šฉ (์„ฑ๋Šฅ ์ตœ์ ํ™” & ์Šค๋ƒ…์ƒท ์ƒ๋žต)
public class UserService {

    private final UserRepository userRepository;

    public UserResponse findById(Long id) {
        User user = userRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: " + id));
        return UserResponse.from(user);
    }

    @Transactional // ์“ฐ๊ธฐ/์ˆ˜์ • ์ž‘์—…์—๋Š” ๋ฐ˜๋“œ์‹œ readOnly = false (๊ธฐ๋ณธ๊ฐ’)
    public Long updateName(Long id, String newName) {
        User user = userRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));
        user.updateName(newName); // ๋”ํ‹ฐ ์ฒดํ‚น(Dirty Checking)์œผ๋กœ save() ํ˜ธ์ถœ ์—†์ด ์ž๋™ UPDATE ์ฟผ๋ฆฌ ๋ฐœ์ƒ
        return user.getId();
    }
}

12. Spring Security 6.x ๊ธฐ๋ณธ ๊ตฌ์„ฑ (Security FilterChain)

// Spring Boot 3.x (Spring Security 6.x ์ตœ์‹  ์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ)
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {

    private final JwtAuthenticationFilter jwtAuthFilter;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            // 1. CSRF & FormLogin ๋น„ํ™œ์„ฑํ™” (Stateless REST API ํ™˜๊ฒฝ)
            .csrf(AbstractHttpConfigurer::disable)
            .formLogin(AbstractHttpConfigurer::disable)
            .httpBasic(AbstractHttpConfigurer::disable)

            // 2. ์„ธ์…˜์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์™„์ „ ๋ฌด์ƒํƒœ(Stateless)๋กœ ์„ค์ •
            .sessionManagement(session ->
                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            )

            // 3. URL ์š”์ฒญ๋ณ„ ์ ‘๊ทผ ๊ถŒํ•œ ์„ค์ •
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/api/v1/auth/**", "/public/**", "/swagger-ui/**", "/v3/api-docs/**").permitAll()
                .requestMatchers("/api/v1/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
            )

            // 4. JWT ํ•„ํ„ฐ๋ฅผ UsernamePasswordAuthenticationFilter ์ด์ „์— ๋ฐฐ์น˜
            .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(); // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋‹จ๋ฐฉํ–ฅ ์•”ํ˜ธํ™”
    }
}

13. ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ & ์Šค์ผ€์ค„๋ง (Async & Scheduling)

// [1] ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํ™œ์„ฑํ™” ๋ฐ ์‹คํ–‰ (@EnableAsync)
@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "mailTaskExecutor")
    public Executor mailTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("async-mail-");
        executor.initialize();
        return executor;
    }
}

@Service
public class EmailService {
    @Async("mailTaskExecutor") // ์ง€์ •๋œ ์Šค๋ ˆ๋“œํ’€์—์„œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋น„๋™๊ธฐ ์‹คํ–‰
    public CompletableFuture<Boolean> sendWelcomeEmail(String toEmail) {
        // ๊ธด ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋Š” ์™ธ๋ถ€ SMTP ๋ฉ”์ผ ๋ฐœ์†ก ๋กœ์ง
        return CompletableFuture.completedFuture(true);
    }
}

// [2] ์ฃผ๊ธฐ์  ์ž‘์—… ์Šค์ผ€์ค„๋ง (@EnableScheduling)
@Configuration
@EnableScheduling
public class SchedulerConfig { }

@Component
public class CacheEvictScheduler {
    // ๋งค์ผ ์ƒˆ๋ฒฝ 3์‹œ ์‹คํ–‰ (์ดˆ ๋ถ„ ์‹œ ์ผ ์›” ์š”์ผ)
    @Scheduled(cron = "0 0 3 * * ?")
    public void runDailyBatch() {
        System.out.println("์ผ์ผ ๋งŒ๋ฃŒ ์บ์‹œ ์ •๋ฆฌ ์‹คํ–‰");
    }

    // ์ด์ „ ์ž‘์—… ์‹œ์ž‘ ๊ธฐ์ค€ 5์ดˆ๋งˆ๋‹ค ์‹คํ–‰
    @Scheduled(fixedRate = 5000)
    public void heartbeat() { }
}

14. ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ (Testing with MockMvc)

// [1] ์ปจํŠธ๋กค๋Ÿฌ ์Šฌ๋ผ์ด์Šค ํ…Œ์ŠคํŠธ (@WebMvcTest)
@WebMvcTest(UserController.class)
class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserService userService;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    @DisplayName("ํšŒ์› ๋‹จ๊ฑด ์กฐํšŒ ์„ฑ๊ณต ์‹œ 200 OK์™€ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค")
    void getUserSuccess() throws Exception {
        // given
        UserResponse response = new UserResponse(1L, "lucky", "lucky@example.com");
        given(userService.findById(1L)).willReturn(response);

        // when & then
        mockMvc.perform(get("/api/v1/users/1")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").value(1L))
                .andExpect(jsonPath("$.name").value("lucky"))
                .andExpect(jsonPath("$.email").value("lucky@example.com"));
    }
}

// [2] ์„œ๋น„์Šค ๋‹จ์œ„ ํ…Œ์ŠคํŠธ (Mockito)
@ExtendWith(MockitoExtension.class)
class UserServiceTest {

    @Mock
    private UserRepository userRepository;

    @InjectMocks
    private UserService userService;

    @Test
    @DisplayName("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์œ ์ € ์กฐํšŒ ์‹œ ResourceNotFoundException์ด ๋ฐœ์ƒํ•œ๋‹ค")
    void userNotFoundThrowsException() {
        // given
        given(userRepository.findById(999L)).willReturn(Optional.empty());

        // when & then
        org.junit.jupiter.api.Assertions.assertThrows(
            ResourceNotFoundException.class,
            () -> userService.findById(999L)
        );
    }
}

15. ์šด์˜ ๋ชจ๋‹ˆํ„ฐ๋ง & ์ตœ์ ํ™” (Actuator & Production)

# Actuator ์„ค์ • (application.yml)
management:
  endpoints:
    web:
      exposure:
        # ์‹ค๋ฌด ๊ถŒ์žฅ: ํ—ฌ์Šค์ฒดํฌ, ์ง€ํ‘œ ๋ฐ ํ”„๋กœ๋ฉ”ํ…Œ์šฐ์Šค๋งŒ ๋…ธ์ถœ (env, beans ๋“ฑ ๋ฏผ๊ฐ ์ •๋ณด ์ˆจ๊น€)
        include: health, info, metrics, prometheus
      base-path: /actuator
  endpoint:
    health:
      show-details: when_authorized # ์ƒ์„ธ ์ƒํƒœ ์ •๋ณด๋Š” ๊ถŒํ•œ ๋ณด์œ ์ž์—๊ฒŒ๋งŒ ๋…ธ์ถœ
      probes:
        enabled: true # ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค Liveness, Readiness ํ”„๋กœ๋ธŒ ์ง€์›

# ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ๋ฅผ ์œ„ํ•œ Graceful Shutdown ์„ค์ •
server:
  shutdown: graceful

spring:
  lifecycle:
    timeout-per-shutdown-phase: 20s # ์ข…๋ฃŒ ์‹œ ์ฒ˜๋ฆฌ ์ค‘์ธ ์š”์ฒญ ๋Œ€๊ธฐ ์‹œ๊ฐ„ (์ตœ๋Œ€ 20์ดˆ)

16. Spring Boot ํ”„๋กœ๋•์…˜ ์„ฑ๋Šฅ ์ตœ์ ํ™” & ์žฅ์•  ๋Œ€์‘ ์ฒดํฌ๋ฆฌ์ŠคํŠธ (Production Optimization & Troubleshooting Checklist)

์‹ค์ œ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ ๊ณ ์„ฑ๋Šฅ ํŠธ๋ž˜ํ”ฝ์„ ์ง€์—ฐ ์—†์ด ์•ˆ์ •์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ 5๋Œ€ ์—”์ง€๋‹ˆ์–ด๋ง ์ฒดํฌ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค:

1. ์ƒ์„ฑ์ž ์ฃผ์ž…(Constructor Injection) ๊ฐ•์ œ & ์ˆœํ™˜ ์ฐธ์กฐ(Circular Dependency) ๋ฐฉ์ง€

  • ํ•„๋“œ ์ฃผ์ž… ๊ธˆ์ง€: @Autowired private UserService userService;์™€ ๊ฐ™์€ ํ•„๋“œ ์ฃผ์ž…์€ ํ…Œ์ŠคํŠธ ์‹œ Mock ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ฃผ์ž…ํ•˜๊ธฐ ์–ด๋ ต๊ณ , ๋ถˆ๋ณ€์„ฑ(final)์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ์‹ค๋ฌด ํ‘œ์ค€: ํด๋ž˜์Šค ๋ ˆ๋ฒจ์— Lombok @RequiredArgsConstructor๋ฅผ ์„ ์–ธํ•˜๊ณ , ๋ชจ๋“  ์˜์กด์„ฑ ํ•„๋“œ๋ฅผ private final๋กœ ์„ ์–ธํ•˜์—ฌ ์ƒ์„ฑ์ž ์‹œ์ ์— ๋ชจ๋“  ์˜์กด์„ฑ์ด ์™„๋ฒฝํ•˜๊ฒŒ ์ถฉ์กฑ๋˜๋„๋ก ๊ฐ•์ œํ•˜์‹ญ์‹œ์˜ค. ์Šคํ”„๋ง ๋ถ€ํŠธ 2.6๋ถ€ํ„ฐ ๊ธฐ๋ณธ ์ฐจ๋‹จ๋œ ๋นˆ ์ˆœํ™˜ ์ฐธ์กฐ(A โž” B โž” A)๋„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘ ๋‹จ๊ณ„์—์„œ ์ฆ‰์‹œ ์ ๋ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. JPA N+1 ๋ฌธ์ œ ์›์ฒœ ์ฐจ๋‹จ

  • ๋ฐœ์ƒ ๋ฉ”์ปค๋‹ˆ์ฆ˜: 1๊ฑด์˜ ๋ถ€๋ชจ ์—”ํ‹ฐํ‹ฐ ๋ชฉ๋ก์„ ์กฐํšŒํ•œ ํ›„ ์ง€์—ฐ ๋กœ๋”ฉ(Lazy Loading)์œผ๋กœ ์„ค์ •๋œ ์—ฐ๊ด€ ์—”ํ‹ฐํ‹ฐ ์ปฌ๋ ‰์…˜์„ ์ฐธ์กฐํ•  ๋•Œ, ๋ถ€๋ชจ ํ–‰ ๊ฐœ์ˆ˜(N)๋งŒํผ ์ถ”๊ฐ€ SELECT ์ฟผ๋ฆฌ๊ฐ€ ์—ฐ์† ์‹คํ–‰๋˜์–ด DB๋ฅผ ๋‹ค์šด์‹œํ‚ค๋Š” ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ์„ฑ๋Šฅ ๊ฒฐํ•จ์ž…๋‹ˆ๋‹ค.
  • ํ•ด๊ฒฐ์ฑ…:
    1. ๋‹จ๊ฑด/์†Œ๊ทœ๋ชจ ์—ฐ๊ด€ ์กฐํšŒ: JPQL JOIN FETCH ๋˜๋Š” @EntityGraph(attributePaths = {"orders"})๋ฅผ ์ ์šฉํ•˜์—ฌ ๋‹จ ํ•œ ๋ฒˆ์˜ ๋‚ด๋ถ€ ์กฐ์ธ์œผ๋กœ ์ผ๊ด„ ๋กœ๋”ฉ.
    2. ๋‹ค๊ฑด ์ปฌ๋ ‰์…˜ ํŽ˜์ด์ง•: application.yml์— spring.jpa.properties.hibernate.default_batch_fetch_size: 100์„ ๋ฐ˜๋“œ์‹œ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค. 100๊ฐœ ๋‹จ์œ„์˜ IN ์ฟผ๋ฆฌ๋กœ ๋ณ‘ํ•ฉ ์‹คํ–‰๋˜์–ด N๋ฒˆ์˜ ์ฟผ๋ฆฌ๊ฐ€ 1๋ฒˆ์˜ ์ฟผ๋ฆฌ๋กœ ํš๊ธฐ์ ์œผ๋กœ ์••์ถ•๋ฉ๋‹ˆ๋‹ค.

3. ์ปจํ…Œ์ด๋„ˆ ๊ธฐ๋ฐ˜ JVM ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ง• & ์ตœ์‹  ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ(GC) ์ ์šฉ

  • ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ(OOMKilled) ๋ฐฉ์ง€: ๋„์ปค/์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํ™˜๊ฒฝ์—์„œ๋Š” -Xmx๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ•˜๊ธฐ๋ณด๋‹ค, cgroups v2๋ฅผ ์ž๋™ ์ธ์‹ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค: -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 (ํŒŒ๋“œ ๋ฉ”๋ชจ๋ฆฌ์˜ 75%๋ฅผ ํž™์œผ๋กœ ํ• ๋‹นํ•˜๊ณ  ๋‚˜๋จธ์ง€ 25%๋Š” ๋ฉ”ํƒ€์ŠคํŽ˜์ด์Šค, ์Šค๋ ˆ๋“œ ์Šคํƒ ๋ฐ ๋„ค์ดํ‹ฐ๋ธŒ ๋ฉ”๋ชจ๋ฆฌ์— ์–‘๋ณด).
  • GC ์„ ํƒ: ์ดˆ์ €์ง€์—ฐ(Sub-millisecond) ์‹ค์‹œ๊ฐ„ API๊ฐ€ ํ•ต์‹ฌ์ด๋ผ๋ฉด Java 21+์˜ ์„ธ๋Œ€๋ณ„ ZGC(-XX:+UseZGC -XX:+ZGenerational)๋ฅผ ์ ๊ทน ๋„์ž…ํ•˜๊ณ , ์ผ๋ฐ˜ ์›น ๋ฐฑ์—”๋“œ๋Š” ๊ธฐ๋ณธ G1GC(-XX:+UseG1GC)์˜ ์ผ์‹œ ์ •์ง€ ๋ชฉํ‘œ ์‹œ๊ฐ„(-XX:MaxGCPauseMillis=200)์„ ์œ ์ง€ํ•˜์‹ญ์‹œ์˜ค.

4. HikariCP ์ปค๋„ฅ์…˜ ๋ˆ„์ˆ˜(Connection Leak) ์กฐ๊ธฐ ๊ฐ์ง€

  • ๋ˆ„์ˆ˜ ์›์ธ: @Transactional ๋ฒ”์œ„ ๋‚ด์—์„œ ์™ธ๋ถ€ ์„œ๋“œํŒŒํ‹ฐ HTTP API(๊ฒฐ์ œ ๋ชจ๋“ˆ, ์™ธ๋ถ€ ์›นํ›… ๋“ฑ)๋ฅผ ๋™๊ธฐ ํ˜ธ์ถœํ•˜๋‹ค ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•˜๋ฉด, ์ปค๋„ฅ์…˜์ด ํ’€์— ๋ฐ˜๋‚ฉ๋˜์ง€ ์•Š๊ณ  ์Šค๋ ˆ๋“œ๊ฐ€ ์˜๊ตฌ ๊ณ ๊ฐˆ๋ฉ๋‹ˆ๋‹ค.
  • ๊ฐ์ง€ ์˜ต์…˜: spring.datasource.hikari.leak-detection-threshold: 2000(2์ดˆ)์„ ํ™œ์„ฑํ™”ํ•˜๋ฉด, 2์ดˆ ์ด์ƒ ์ปค๋„ฅ์…˜์„ ์ฅ๊ณ  ์žˆ๋Š” ํŠธ๋žœ์žญ์…˜์˜ ์Šคํƒ ํŠธ๋ ˆ์ด์Šค๋ฅผ ์ฝ˜์†” ๋กœ๊ทธ์— ์ฆ‰์‹œ ๊ฒฝ๊ณ ๋กœ ์ถœ๋ ฅํ•ด ์žฅ์•  ์œ„์น˜๋ฅผ ์ถ”์ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์›์น™: ์™ธ๋ถ€ ๋„คํŠธ์›Œํฌ I/O ์ž‘์—…์€ ํŠธ๋žœ์žญ์…˜ ๋ฉ”์„œ๋“œ ๋ฐ–์—์„œ ์„ ํ–‰ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ๋น„๋™๊ธฐ(CompletableFuture)๋กœ ๋ถ„๋ฆฌํ•˜์‹ญ์‹œ์˜ค.

5. Actuator ์—”๋“œํฌ์ธํŠธ ๋ณด์•ˆ ๊ฒฉ๋ฆฌ

  • /actuator/env, /actuator/heapdump, /actuator/configprops ์—”๋“œํฌ์ธํŠธ๊ฐ€ ๊ณต์šฉ ์ธํ„ฐ๋„ท์— ๋…ธ์ถœ๋  ๊ฒฝ์šฐ DB ๋น„๋ฐ€๋ฒˆํ˜ธ, JWT ์‹œํฌ๋ฆฟ ํ‚ค ๋“ฑ ์ค‘๋Œ€ํ•œ ๋ณด์•ˆ ์ธ์ฆ ์ •๋ณด๊ฐ€ ์œ ์ถœ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” management.endpoints.web.exposure.include: health, info, metrics, prometheus๋งŒ ์„ ๋ณ„ ๋…ธ์ถœํ•˜๊ณ , ๋‚ด๋ถ€ ๊ด€๋ฆฌ๋ง ํฌํŠธ(management.server.port: 8081)๋กœ ๋ถ„๋ฆฌํ•˜๊ฑฐ๋‚˜ Spring Security๋กœ ์ ‘๊ทผ ๊ถŒํ•œ์„ ์ฒ ์ €ํžˆ ํ†ต์ œํ•˜์‹ญ์‹œ์˜ค.
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500