213 lines
7.2 KiB
Markdown
213 lines
7.2 KiB
Markdown
아래는 Spring Boot의 기본 설정 및 의존성 관리에 대해 실무 중심으로 설명한 글입니다. SEO를 고려해 키워드를 적절히 배치하고, 흥미로운 주제를 앞부분에 배치하며, 읽기 쉽게 구성했습니다. 주요 의존성을 정리하고 설정 방법을 코드 예제와 함께 설명했으며, Gradle은 Kotlin DSL(`build.gradle.kts`)로 작성했습니다.
|
|
|
|
---
|
|
|
|
## Spring Boot 기본 설정과 의존성 관리: 실무에서 바로 써먹는 가이드
|
|
|
|
Spring Boot는 복잡한 설정을 줄이고 개발을 빠르게 시작할 수 있게 해주는 프레임워크입니다. 하지만 실무에서 효율적으로 사용하려면 **기본 설정**과 **의존성 관리**를 제대로 이해해야 하죠. 이 글에서는 **Spring Boot 설정 방법**, **주요 의존성 정리**, 그리고 각 의존성의 설정 예제를 다룹니다. "Spring Boot 의존성 관리"나 "Spring Boot 설정" 같은 키워드로 검색하는 개발자라면 꼭 읽어보세요!
|
|
|
|
### 1. Spring Boot 기본 설정: 시작은 이렇게!
|
|
Spring Boot는 기본 설정을 `application.properties` 또는 `application.yml` 파일로 관리합니다. YAML 형식이 가독성이 좋아 실무에서 더 자주 사용되죠. 기본 설정 파일은 `src/main/resources` 폴더에 위치합니다.
|
|
|
|
#### 기본 설정 예제 (application.yml)
|
|
```yaml
|
|
server:
|
|
port: 8080
|
|
spring:
|
|
application:
|
|
name: my-app
|
|
datasource:
|
|
url: jdbc:h2:mem:testdb
|
|
driver-class-name: org.h2.Driver
|
|
username: sa
|
|
password:
|
|
logging:
|
|
level:
|
|
root: INFO
|
|
com.example: DEBUG
|
|
```
|
|
|
|
**주요 설정 항목**:
|
|
- `server.port`: 애플리케이션 포트 (기본값: 8080).
|
|
- `spring.application.name`: 애플리케이션 이름 (마이크로서비스에서 유용).
|
|
- `spring.datasource`: 데이터베이스 연결 설정.
|
|
- `logging.level`: 로그 레벨 조정.
|
|
|
|
**실무 팁**:
|
|
- 환경별 설정 분리: `application-dev.yml`, `application-prod.yml`로 나눠 관리하세요. 실행 시 `-Dspring.profiles.active=dev`로 선택 가능.
|
|
- 민감 정보는 환경 변수나 외부 설정 관리 도구(Vault 등)로 처리하세요.
|
|
|
|
### 2. 의존성 관리: Spring Boot의 강력한 무기
|
|
Spring Boot는 `spring-boot-starter`라는 의존성 번들로 필요한 라이브러리를 쉽게 추가할 수 있습니다. Gradle이나 Maven으로 관리하며, 버전 충돌 걱정 없이 사용할 수 있도록 자동으로 호환 버전을 맞춰줍니다.
|
|
|
|
#### Gradle 설정 (build.gradle.kts)
|
|
```kotlin
|
|
plugins {
|
|
id("org.springframework.boot") version "3.2.3"
|
|
id("io.spring.dependency-management") version "1.1.4"
|
|
java
|
|
}
|
|
|
|
group = "com.example"
|
|
version = "0.0.1-SNAPSHOT"
|
|
|
|
repositories {
|
|
mavenCentral()
|
|
}
|
|
|
|
dependencies {
|
|
implementation("org.springframework.boot:spring-boot-starter")
|
|
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
|
}
|
|
|
|
tasks {
|
|
test {
|
|
useJUnitPlatform()
|
|
}
|
|
}
|
|
```
|
|
|
|
- `spring-boot-starter`: 기본 의존성 포함 (Spring Core, Logging 등).
|
|
- `io.spring.dependency-management`: 의존성 버전 관리 플러그인.
|
|
|
|
### 3. 주요 의존성과 설정 방법
|
|
Spring Boot에서 자주 사용하는 의존성과 설정 방법을 정리했습니다. 실무에서 꼭 필요한 것들만 골라봤어요.
|
|
|
|
#### 3.1. Spring Web (REST API 및 MVC)
|
|
- **의존성**: `spring-boot-starter-web`
|
|
- **설정**: 기본적으로 내장 Tomcat 서버가 포함되며, 추가 설정 없이 REST API를 만들 수 있습니다.
|
|
|
|
```kotlin
|
|
dependencies {
|
|
implementation("org.springframework.boot:spring-boot-starter-web")
|
|
}
|
|
```
|
|
|
|
**예제**:
|
|
```java
|
|
@RestController
|
|
@RequestMapping("/api")
|
|
public class HelloController {
|
|
@GetMapping("/hello")
|
|
public String hello() {
|
|
return "Hello, Spring Boot!";
|
|
}
|
|
}
|
|
```
|
|
|
|
#### 3.2. Spring Data JPA (데이터베이스 연동)
|
|
- **의존성**: `spring-boot-starter-data-jpa`
|
|
- **추가 의존성**: 데이터베이스 드라이버 (예: H2, MySQL).
|
|
|
|
```kotlin
|
|
dependencies {
|
|
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
|
|
runtimeOnly("com.h2database:h2") // H2 인메모리 DB
|
|
}
|
|
```
|
|
|
|
**설정 (application.yml)**:
|
|
```yaml
|
|
spring:
|
|
datasource:
|
|
url: jdbc:h2:mem:testdb
|
|
driver-class-name: org.h2.Driver
|
|
username: sa
|
|
password:
|
|
jpa:
|
|
hibernate:
|
|
ddl-auto: update
|
|
show-sql: true
|
|
```
|
|
|
|
**실무 팁**:
|
|
- `ddl-auto`: 개발 시 `update`, 운영 시 `none`으로 설정.
|
|
- 성능을 위해 `@Entity`에 `fetch = FetchType.LAZY`를 기본으로 사용하세요.
|
|
|
|
#### 3.3. Spring Boot Actuator (모니터링)
|
|
- **의존성**: `spring-boot-starter-actuator`
|
|
- **설정**: 애플리케이션 상태를 모니터링할 수 있는 엔드포인트 제공.
|
|
|
|
```kotlin
|
|
dependencies {
|
|
implementation("org.springframework.boot:spring-boot-starter-actuator")
|
|
}
|
|
```
|
|
|
|
**설정 (application.yml)**:
|
|
```yaml
|
|
management:
|
|
endpoints:
|
|
web:
|
|
exposure:
|
|
include: health, info, metrics
|
|
```
|
|
|
|
- `/actuator/health`, `/actuator/metrics` 같은 엔드포인트로 상태 확인 가능.
|
|
|
|
#### 3.4. Lombok (코드 간소화)
|
|
- **의존성**: `lombok`
|
|
- **설정**: 어노테이션 프로세서를 추가해 IDE에서 동작하도록 설정.
|
|
|
|
```kotlin
|
|
dependencies {
|
|
implementation("org.projectlombok:lombok")
|
|
annotationProcessor("org.projectlombok:lombok")
|
|
}
|
|
```
|
|
|
|
**예제**:
|
|
```java
|
|
@Data
|
|
@AllArgsConstructor
|
|
public class User {
|
|
private Long id;
|
|
private String name;
|
|
}
|
|
```
|
|
|
|
#### 3.5. Spring Security (인증 및 권한)
|
|
- **의존성**: `spring-boot-starter-security`
|
|
- **설정**: 기본적으로 모든 요청에 인증 요구.
|
|
|
|
```kotlin
|
|
dependencies {
|
|
implementation("org.springframework.boot:spring-boot-starter-security")
|
|
}
|
|
```
|
|
|
|
**설정 예제**:
|
|
```java
|
|
@Configuration
|
|
@EnableWebSecurity
|
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
@Override
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
http
|
|
.authorizeRequests()
|
|
.antMatchers("/public/**").permitAll()
|
|
.anyRequest().authenticated()
|
|
.and()
|
|
.formLogin();
|
|
}
|
|
}
|
|
```
|
|
|
|
**실무 팁**: JWT나 OAuth2를 추가로 연동하면 토큰 기반 인증도 가능합니다.
|
|
|
|
### 4. 의존성 관리 팁
|
|
- **버전 명시 피하기**: `spring-boot-starter`가 제공하는 기본 버전을 사용하세요.
|
|
- **BOM 활용**: `spring-boot-dependencies` BOM(Bill of Materials)이 호환성을 보장합니다.
|
|
- **의존성 충돌 확인**: `./gradlew dependencies`로 확인 후 불필요한 의존성 제거.
|
|
|
|
### 마무리
|
|
Spring Boot의 기본 설정과 의존성 관리는 실무에서 생산성을 크게 높여줍니다. 이 글에서 다룬 **기본 설정 파일**, **주요 의존성**, **설정 방법**을 참고해 프로젝트를 시작해보세요. "Spring Boot 설정"으로 검색했다면 이 글이 딱 맞을 거예요. 궁금한 점은 댓글로 남겨주세요!
|
|
|
|
---
|
|
|
|
### SEO 및 가독성 반영
|
|
- **키워드**: "Spring Boot 기본 설정", "Spring Boot 의존성 관리", "Spring Boot 설정 방법" 등 자연스럽게 배치.
|
|
- **흥미로운 주제 전반부**: 기본 설정과 의존성 관리의 중요성을 앞부분에 강조.
|
|
- **가독성**: 소제목, 코드 블록, 짧은 문장으로 구성.
|
|
|
|
추가 요청이나 보완할 내용이 있다면 말씀해주세요! |