반응형
Spring Boot에서 application.yml 파일을 설정하는 방법
Spring Boot에서 application.yml
파일은 애플리케이션의 설정 정보를 관리하는 데 사용됩니다. YAML 형식은 읽기 쉽고 가독성이 좋아 JSON이나 Properties 파일보다 선호되기도 합니다. 이 글에서는 application.yml
파일의 기본 사용법과 주요 설정 방법을 다룹니다.
1. YAML 파일의 기본 구조
YAML은 계층 구조를 들여쓰기로 표현하며, 키: 값
형식으로 데이터를 저장합니다.
예제:
server:
port: 8080
servlet:
context-path: /api
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: password
설명:
server.port
: 애플리케이션이 실행될 포트를 지정합니다.server.servlet.context-path
: 애플리케이션의 기본 URL 경로를 설정합니다.spring.datasource
: 데이터베이스 연결 정보를 설정합니다.
2. application.yml
파일 생성 위치
application.yml
파일은 src/main/resources
디렉토리에 생성해야 합니다. Spring Boot는 애플리케이션 실행 시 이 파일을 자동으로 읽어 설정을 적용합니다.
3. 주요 설정 예제
- 서버 설정
server:
port: 8081
servlet:
context-path: /myapp
port
: 애플리케이션이 실행될 포트를 설정합니다.context-path
: URL 경로를 변경합니다.
- 데이터베이스 설정
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
url
: 데이터베이스 연결 URLusername
,password
: 데이터베이스 인증 정보ddl-auto
: Hibernate의 DDL 정책 설정 (update
,create
,validate
등)show-sql
: 실행된 SQL을 콘솔에 출력
- 프로파일 설정
Spring Boot는 환경에 따라 설정을 변경할 수 있도록 프로파일 기능을 제공합니다.
application.yml
기본 설정:
spring:
profiles:
active: dev
application-dev.yml
(개발 환경):
server:
port: 8082
application-prod.yml
(운영 환경):
server:
port: 8083
Spring Boot는 spring.profiles.active
에 지정된 프로파일에 따라 설정 파일을 선택적으로 로드합니다.
- 로그 설정
logging:
level:
root: info
com.example: debug
file:
name: logs/application.log
root
: 전체 애플리케이션의 로그 레벨 설정com.example
: 특정 패키지의 로그 레벨 설정file.name
: 로그 파일 저장 경로
- 메일 설정
spring:
mail:
host: smtp.gmail.com
port: 587
username: example@gmail.com
password: password
properties:
mail:
smtp:
auth: true
starttls:
enable: true
- 메일 서버와 관련된 설정을 구성합니다.
4. 배열과 리스트 처리
YAML 파일에서 배열은 -
기호를 사용해 정의할 수 있습니다.
예제:
topics:
- spring-boot
- java
- microservices
자바 코드에서 읽기:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class TopicsConfig {
@Value("${topics}")
private List<String> topics;
public List<String> getTopics() {
return topics;
}
}
5. 자바 객체로 매핑하기
Spring Boot에서는 @ConfigurationProperties
를 사용하여 YAML 설정을 객체로 매핑할 수 있습니다.
YAML 설정:
app:
name: MyApplication
version: 1.0.0
authors:
- Alice
- Bob
Java 클래스:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String version;
private List<String> authors;
// Getter와 Setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public List<String> getAuthors() {
return authors;
}
public void setAuthors(List<String> authors) {
this.authors = authors;
}
}
반응형
'Spring Boot' 카테고리의 다른 글
Spring Boot에서 데이터 유효성(Validation) 검사를 적용하는 방법 (0) | 2025.01.08 |
---|---|
Spring Boot에서 JSON 데이터를 보내고 받는 방법 (0) | 2025.01.08 |
Postman으로 Spring Boot API를 테스트하는 방법 (0) | 2025.01.08 |
Spring Boot API Server 개발에서 GET / POST / PUT / DELETE 요청 처리하는 방법 (0) | 2025.01.08 |
Spring Boot에서 컨트롤러(@RestController)를 사용하는 방법 (0) | 2025.01.08 |