[Spring] 개요: Framework, Boot, Cloud
정의
Spring은 Java 엔터프라이즈 표준 프레임워크 그룹. 2003년 Rod Johnson이 EJB 대안으로 시작. IoC/DI가 핵심. 현재는:
- Spring Framework: 기본 (IoC, AOP, MVC, JDBC, Transaction)
- Spring Boot: 자동 설정으로 즉시 실행 가능한 앱 구성
- Spring Cloud: 마이크로서비스 지원
- Spring Data: 데이터 접근 추상화 (JPA, MongoDB, Redis, …)
- Spring Security: 인증·인가
- Spring Batch: 배치 처리
신규 프로젝트는 거의 모두 Spring Boot 시작.
Spring Boot 빠른 시작
# Spring Initializr
# https://start.spring.io 또는 IDE
// build.gradle.kts
plugins {
java
id("org.springframework.boot") version "3.4.0"
id("io.spring.dependency-management") version "1.1.6"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
// MyApplication.java
package com.example.myapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
// HelloController.java
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring!";
}
}
./gradlew bootRun
핵심 개념
IoC (Inversion of Control)
객체 생성/생명주기를 컨테이너가 관리. 코드가 “이 객체 줘”라고 요청, 컨테이너가 적절한 인스턴스 주입.
DI (Dependency Injection)
객체가 자기 의존성을 직접 만들지 않고 외부에서 주입받음.
@Service
public class UserService {
private final UserRepository userRepository;
// 생성자 주입 (권장)
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User findById(Long id) {
return userRepository.findById(id).orElseThrow();
}
}
UserService를 사용하는 코드는 UserRepository 인스턴스 어떻게 만들지 몰라도 됨.
Bean
Spring 컨테이너가 관리하는 객체. @Component, @Service, @Repository, @Controller, @RestController, @Configuration 등으로 등록.
ApplicationContext
Bean 컨테이너. 모든 Bean의 생명주기 관리, DI 수행, 이벤트 publish.
AOP
cross-cutting concern (로깅, 트랜잭션, 보안)을 모듈화. @Aspect로.
Spring Boot 자동 구성
@SpringBootApplication은 셋 합친 것:
@Configuration: 설정 클래스@EnableAutoConfiguration: classpath 기반 자동 설정@ComponentScan: 이 패키지 이하의@Component자동 등록
classpath에 spring-boot-starter-data-jpa가 있으면:
- DataSource 자동 구성
- EntityManager 자동 구성
- 트랜잭션 매니저 자동 구성
application.properties로 세부 조정:
spring.datasource.url=jdbc:postgresql://localhost:5432/myapp
spring.datasource.username=myapp
spring.datasource.password=...
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
server.port=8080
Starter dependencies
spring-boot-starter-*로 한 줄에 필요한 lib 묶음.
| Starter | 포함 |
|---|---|
web | Tomcat + Spring MVC + Jackson |
webflux | Reactor + Netty |
data-jpa | JPA + Hibernate |
data-redis | Redis client |
security | Spring Security |
validation | Bean Validation |
actuator | 모니터링 endpoint |
test | JUnit + Mockito + AssertJ |
버전 정책
| 버전 | 출시 | 지원 | 비고 |
|---|---|---|---|
| 3.0 | 2022-11 | LTS | Jakarta EE 9 + Java 17 |
| 3.1 | 2023-05 | ||
| 3.2 | 2023-11 | ||
| 3.3 | 2024-05 | ||
| 3.4 | 2024-11 | 현재 stable | |
| 3.5 | 2025-05 | LTS 후보 |
Java 17+, Jakarta EE namespace(jakarta.*). Spring 5.x 호환성 깨짐 → migration tool 필요.
Spring vs Spring Boot 차이
| Spring Framework | Spring Boot | |
|---|---|---|
| 출발점 | XML/Java config 수동 | 자동 설정 |
| 서버 | 별도 (Tomcat 등) | 내장 (embedded Tomcat) |
| 패키징 | WAR | JAR (executable) |
| 설정 | 명시적 | convention |
| 학습 곡선 | 가파름 | 부드러움 |
새 프로젝트는 Boot. 레거시 Spring Framework도 여전히 흔함.
Spring 생태계
Spring Boot
├── Spring Framework (core)
│ ├── Core Container (IoC, DI)
│ ├── AOP
│ ├── Spring MVC / WebFlux
│ ├── JDBC, ORM, Transaction
│ └── Test
├── Spring Data
│ ├── JPA
│ ├── MongoDB
│ ├── Redis
│ ├── Elasticsearch
│ └── R2DBC (reactive)
├── Spring Security
│ ├── Authentication
│ ├── OAuth2 / JWT
│ └── Method security
├── Spring Cloud
│ ├── Config Server
│ ├── Service Discovery (Eureka)
│ ├── Gateway
│ ├── Circuit Breaker (Resilience4j)
│ └── OpenFeign
├── Spring Batch
├── Spring Integration
├── Spring Kafka / AMQP
└── Spring Native (GraalVM)
디렉터리 구조 (관례)
src/
├── main/
│ ├── java/com/example/myapp/
│ │ ├── MyApplication.java
│ │ ├── controller/
│ │ ├── service/
│ │ ├── repository/
│ │ ├── domain/
│ │ ├── dto/
│ │ ├── config/
│ │ └── exception/
│ └── resources/
│ ├── application.yml
│ ├── static/
│ ├── templates/
│ └── db/migration/
└── test/
└── java/com/example/myapp/
도메인 단위 패키지(user/, order/)가 더 좋은 패턴이라는 의견(DDD).
Spring과 Java vs Kotlin
Kotlin은 Spring의 1급 시민. data class, null safety, extension function이 Spring과 잘 어울림.
@Entity
data class User(
@Id @GeneratedValue val id: Long? = null,
val email: String,
val name: String,
)
@Service
class UserService(private val userRepository: UserRepository) {
fun findByEmail(email: String): User =
userRepository.findByEmail(email) ?: throw NotFoundException()
}
3.0+에선 Kotlin 비즈니스 로직 + Java 인프라가 흔함.
학습 경로
- Spring Boot 빠른 시작 + REST API
- Spring Data JPA (DB 접근)
- Spring Security (인증)
- Spring Test (단위 + 통합)
- AOP, custom configuration
- WebFlux (reactive)
- Spring Cloud (마이크로서비스)
- Actuator, 모니터링
다른 Java 프레임워크와 비교
| Spring Boot | Quarkus | Micronaut | Helidon | |
|---|---|---|---|---|
| 시작 시간 | 보통 | 빠름 (Native) | 빠름 | 빠름 |
| 메모리 | 많음 | 적음 | 적음 | 적음 |
| GraalVM | 부분 | 우선 | 우선 | 우선 |
| 생태계 | 매우 큼 | 큼 | 중 | 작음 |
| 학습 자료 | 매우 많음 | 많음 | 중 | 적음 |
서버리스/CLI/마이크로 → Quarkus/Micronaut. 일반 엔터프라이즈 → Spring Boot.
💬 댓글