Spring boot 개발환경에 따른 yml 설정 변경

환승의 개발로그·2021년 7월 13일
0

문제점: JPA 매핑 방식에 있어서 ddl-auto를 개발 환경에서는 create-drop으로 하고 운영 환경에서는 none으로 하고 싶은데 이것을 설정하기 위해서는 어떻게 해야되는지 문제점이 발생하였다.

해결방법: spring profiles active를 통해서 해결한다.

예시

spring:
  datasource:
    hikari:
      maximum-pool-size: 4
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: pass
    platform: postgres
  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        default_schema: db

jwt:
  secret: "12345678901234567890123456789010"

기존 yml 파일



spring:
  profiles:
    active: dev
---

spring:
  profiles: dev
  datasource:
    hikari:
      maximum-pool-size: 4
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: pass
    platform: postgres
  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        default_schema: db

jwt:
  secret: "12345678901234567890123456789010"

---
spring:
  profiles: PROD
  datasource:
    hikari:
      maximum-pool-size: 4
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: pass
    platform: postgres
  jpa:
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        default_schema: db

jwt:
  secret: "12345678901234567890123456789010"

변경된 yml 파일

기존 yml 파일에서 spring.profiles.active 속성을 추가시키고 --- 를 통해서 dev 와 prod를 나누어 yml 파일을 작성한다.

실행 화면에는

이미지와 같이 dev 환경으로 실행이 되었다고 로그에 뜬다.

profile
눈보다는 손으로 기억하기

0개의 댓글