본문 바로가기
Spring

Spring profile

by runlearn 2023. 7. 9.

Spring application profile 설정

애플리케이션 설정을 환경(local, develop, production 등..)에 따라 다르게 적용할 때 profile 옵션을 사용할 수 있다.

profile 설정은 Sprign boot 2.4 버전을 기점으로 설정 방식이 달라진다.

.yml 기준으로 설명을 진행한다. 참고로 yml의 경우 profile에 --- 구분자를 사용하여 논리적으로 파일을 구분할 수 있다.

Spring boot 2.4 이전 profile

application.yml
# default
spring:
    profiles:
        active: local
---
spring:
    profiles: local
# ...
---
spring:
    profiles: dev
# ...
---
spring:
    profiles: prod
# ...

또한, 여러 profile을 포함시키기 위해 include를 이용하여 아래와 같이 설정 할 수 있었다.

application.yml
# default
spring:
    profiles:
        active: local
---
spring:
    profiles: local
        include:
        - common
# ... 

Spring boot 2.4 이후 profile

  • Spring boot 2.4 이후로 spring.profiles는 deprecated 되고 spring.config.active.on-profile 를 사용하여 profile 옵션을 사용한다.
application.yml
spring:
  profiles:
    active: local # 기본적으로 활성화할 profile을 local로 설정 

---
spring:
  config:
    activate:
      on-profile: local
# ... 
---
spring:
  config:
    activate:
      on-profile: prod
# ...       

또한 이전에 include를 사용하여 다른 profile 설정을 추가하는 대신 spring.profiles.group 옵션으로 다른 profile를 포함하여 하나의 profile를 구성할 수 있다.

application.yml (group 사용 예시)
# default
spring:
  profiles:
    active: local # default
    group:
      local:  # local, common profile을 그룹지어 함께 어플리케이션 구동    
        - common
      prod:   # prod, common profile을 그룹지어 함께 어플리케이션 구동  
        - common   

---
spring:
  config:
    activate:
      on-profile: common   # application-common.yml 과 동일한 역할

---
spring:
  config:
    activate:
      on-profile: local

---
spring:
  config:
    activate:
      on-profile: prod

java 옵션으로 profile 설정하기

자바로 실행할 때 VM arguments로 java -jar -Dspring.profiles.active=local app.jar 와 같이 설정을 할 수 있다.

출처 )

https://wonyong-jang.github.io/spring/2022/08/11/Spring-Profile.html

'Spring' 카테고리의 다른 글

[JPA] 프록시  (1) 2023.10.09
[JPA]영속성 컨텍스트  (0) 2023.09.24
[JPA] 더티체킹  (0) 2023.09.11
Spring boot에서 Redis 연동  (0) 2023.07.24

댓글