웹 개발

Spring boot Profile 설정

노루아부지 2021. 1. 1. 20:41

maven 프로젝트에서는 <profile> 태그를 이용하여 profile을 설정했습니다.

하지만 Spring Boot를 이용한다면 Spring Boot를 이용하면 application.properties 파일을 이용하여 손쉽게 profile을 설정할 수 있습니다.

 

1. application.properties 파일을 이용할 경우

  • 아래와 같이 application-{profile}.properties 형식으로 여러 개의 파일을 생성하면 됩니다.
application-default.properties
application-dev.properties
application-prod.properties

 

2. yaml 파일을 이용할 경우

  • 한 파일에 profile 설정이 가능합니다.
  • @PropertySource 애노테이션으로 값을 읽어 올 수 없는 단점이 있습니다.
  • --- 로 구분합니다.
spring:
    profiles:
        active: default
logging:
    level:
        root: info

---

spring:
    profiles:
        active: dev
logging:
    level:
        root: debug

---

spring:
    profiles:
        active: test
logging:
    level:
        root: debug


 

 

3. 실행 예시

  • 아래와 같이 -Dspring.profiles.active 옵션으로 profile을 지정합니다.
  • 지정하지 않을 경우 default로 동작합니다.
java -jar -Dspring.profiles.active=dev ./demo.jar
728x90
loading