YAML (yml)
스프링에서는 환경 변수, 데이터 소스 (DataSource) 등 프로그램이 돌아갈 때 필요한 정보들을 보관할 수 있도록 application.properties 또는 application.yaml을 지원한다. 그런데 개발하는 환경이 달라질 때 마다 이들의 값들을 매번 바꾸는 것은 귀찮은 작업이 되기도 한다.
분리 이전 사용 예시
예시로, 실제 개발 환경에는 MySQL을 쓰고 ddl-auto가 none으로 되는 게 맞지만, 로컬 개발 환경에서는 매번 확인해보는 게 좋기 때문에 h2 인메모리 데이터베이스를 쓰면서 ddl-auto를 create로 하는 게 적합할 것이다.
application 설정 (분기 처리)
•
application.yml
server:
port: 8080
spring:
profiles:
default: local
---
spring:
config:
activate:
on-profile: dev
---
spring:
config:
activate:
on-profile: local
---
spring:
config:
activate:
on-profile: test
YAML
복사
•
application-dev.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost/dodream
username: root
password: Minsung1234
jpa:
generate-ddl: true
show-sql: true
open-in-view: false
sql:
init:
mode: never
YAML
복사
•
application-local.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/testdb
username: root
password: Minsung1234
jpa:
open-in-view: true
hibernate:
ddl-auto: create
properties:
hibernate:
show_sql: true
format_sql: true
dialect: org.hibernate.dialect.MySQL8InnoDBDialect
sql:
init:
mode: never
YAML
복사
•
application-test.yml
spring:
jpa:
open-in-view: true
hibernate:
ddl-auto: create
properties:
hibernate:
show_sql: true
format_sql: true
datasource:
url: jdbc:h2:mem:testdb
username: sa
h2:
console:
enabled: true
YAML
복사