Spring/문법

Spring Boot 환경별 설정 관리하기 – application-{profile}.properties

가지코딩 2025. 5. 23. 11:46

Spring Boot 환경별 설정 관리하기

application.properties 파일에 접미사를 붙여 다양한 환경(개발, 운영 등)에 맞는 설정을 관리하는 방법

에 대해 정리하고자 한다.


1. application-{profile}.properties

  • Spring Boot에서는 다음과 같은 형태로 설정 파일을 분리할 수 있다.
src/main/resources/
├── application.properties		# (공통 설정)
├── application-dev.properties		# (개발 환경)
└── application-prod.properties		# (운영 환경)

 

 

파일 구성 예시

# application.properties (공통 설정)
server.port=8080
spring.profiles.active=dev
# application-dev.properties
server.port=8081
custom.message=This is development
# application-prod.properties
server.port=80
custom.message=This is production

2. profile 활성화 방법

 

방법 1: application.properties에 직접 설정

spring.profiles.active=dev

 

 

방법 2: 실행 시 JVM 옵션 지정

java -jar myapp.jar --spring.profiles.active=prod

 

 

방법 3: 환경 변수 지정

export SPRING_PROFILES_ACTIVE=prod

3. 코드에서 설정값 사용하기

@Value("${custom.message}")
private String message;

 

* 현재 활성화된 profile이 dev라면 위 변수에는 "개발 환경입니다."가 주입된다.


4. 참고 사항

 

  • application.properties는 기본 설정 파일이며, spring.profiles.active를 통해 특정 접미사(profiles)를 활성화하면 해당 파일이 우선 적용된다.
  • 동일한 키가 존재할 경우 application-{profile}.properties 값이 우선

5. 활용 사례

 

데이터베이스 연결 정보 분리

  • 개발 환경은 H2 DB, 운영은 MySQL을 사용해야 할 경우.
# application-dev.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

# application-prod.properties
spring.datasource.url=jdbc:mysql://prod-db-server:3306/app
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=realuser
spring.datasource.password=securePassword!

 

 

로그 레벨 분리

  • 개발 환경에서는 디버깅을 위해 DEBUG 레벨 로그가 필요하지만, 운영 환경에서는 WARN 이상만 출력하고 싶을 경우.
# application-dev.properties
logging.level.root=DEBUG

# application-prod.properties
logging.level.root=WARN

 

 

외부 API 테스트용 URL 분리

  • 테스트 환경은 외부 Mock API, 운영 환경은 실제 API를 사용해야 할 경우.
# application-dev.properties
external.api.url=https://mock-api.dev.example.com

# application-prod.properties
external.api.url=https://api.realvendor.com