
🔗 참고
properties˗ˋˏ와ˎˊ˗ yml의 대표적인 차이는 내부 구조에 있습니다.
# DB Setting
spring.datasource.dirver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=username
spring.datasource.password=password
# Server Port
server.port=5555# DB Setting
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:orcl
username: username
password: password
# Server Port
server:
port: 5555같은 내용을 담고 있지만, 차이점이 보이는데, properties에서는 spring.datasource가 반복되고, yml에서는 계층구조를 표현해주는 것으로 대체된다는 것을 확인할 수 있습니다.
다시 말하자면 yml에서는 공통되는 구조는 상위에 한 번만 작성하고, 하위에 다시 공통되는 구조가 작성되는 형태로 작성한 후, 마지막에 최종적인 값을 설정하는 형태로 구성됩니다. properties에서는 같은 구조 내부의 설정이더라도 구조 전체를 작성해야한다는 점과 비교했을 때 장점으로 작용되는 부분입니다.
리스트 구조에서도 차이점이 있는데, properties에서는 하나씩 설정해줘야하고, yml에서는 계층 구조로 나타내면 됩니다.
application.servers[0].ip=127.0.0.1
application.servers[0].path=/path1
application.servers[1].ip=127.0.0.2
application.servers[1].path=/path2application:
servers:
- ip: '127.0.0.1'
path: '/path1'
- ip: '127.0.0.2'
path: '/path2'정답은 없습니다. 본인이 사용하기 편한 걸 사용하면 되는데요, 주의해야할 점 한 가지는 properties˗ˋˏ와ˎˊ˗ yml을 함께 사용하는 경우입니다. properties 파일이 yml 파일보다 우선순위가 높기 때문에 내용이 덮어쓰워질 수 있습니다. 따라서, 어떤 것을 사용해도 상관은 없지만, 둘 중 한 쪽만 사용하는 것을 추천합니다.
yaml과 yml은 동일한 확장자입니다. 공식확장자는 yaml인데 Once Upon a time.. 호랑이 담배( 🚬(›´-… 확장자의 길이가 3자로 제한되던 시절.. yml로 작성을 하던 것이 지금도 사용되는 거라고 생각해주시면 됩니다!‹ ) 호옵- 🚬 (›´0‹ ) 후우-)피던 시절에
🔗 참고
먼저, 우리가 세미나에서 사용했던 yml 파일을 살펴보겠습니다.
[2차 세미나]
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:tcp://localhost/~/test
username: sa
password:
jpa:
show-sql: true
hibernate:
ddl-auto: create
properties:
hibernate:
format_sql: true
show_sql: true
[3차 세미나]
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/<DB이름>
username: <user 이름>
password: <PW>
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
show_sql: true
[7차 세미나]
spring:
profiles:
active: set1
---
spring:
config:
activate:
on-profile: set1
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://<-RDS 주소->:5432/<- DB 이름 ->
username: <-RDS 사용자 이름->
password: <-RDS 사용자 비밀번호->
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
show_sql: true
server:
port: 8081
---
spring:
config:
activate:
on-profile: set2
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://<-RDS 주소->:5432/<- DB 이름 ->
username: <-RDS 사용자 이름->
password: <-RDS 사용자 비밀번호->
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
show_sql: true
server:
port: 8082
7차 세미나에서는 --- 를 사용해 각 프로파일을 구분하여 작성해준 것을 확인할 수 있습니다.
만약, 구분해서 작성하지 않고, 하나의 yml(yaml) 파일만을 작성한 상태라면 상황에 맞춰서 계속 포트번호를 변경해야하니까 불편할 수 있습니다.
특히, 일반적으로 프로젝트를 진행할 때 데이터베이스나 jpa에서 ddl-auto 설정이 상황에 따라 바뀌는 경우가 많은데, 이 부분을 매번 바꿔주는 것은 불편하고, 주석으로 대체한다고 해도 깔끔하지 않고 실수가 생길 수 있다는 단점이 있기 때문에 파일을 분리해서 작성하는 것이 좋습니다.
세미나에서 --- 를 사용하는 방법을 해봤으니, 파일을 구분해서 yml을 작성하는 방법을 살펴보겠습니다. 두 방법의 기능적 측면은 차이가 없고, 가독성에서의 차이라고 생각해주시면 될 것 같습니다.
보통 prod, dev, local로 구분을 하게 되는데, 아직 운영 중인 경우가 없다면, 우선은 dev˗ˋˏ와ˎˊ˗ local만을 나눠줘도 됩니다.
[1 - 파일 이름 설정]
우리는 스프링 기반의 프로젝트를 하기 때문에, application-{profile}.yml 형태의 이름을 지정해주면 됩니다. {profile} 자리에 원하는 프로파일 이름을 적어주면 되는데요, application-dev.yml 또는 application-local.yml 과 같이 해주시면 됩니다.
[2 - 파일 위치 설정]
파일을 한 디렉토리에 넣어둬도 되고, 다른 디렉토리로 구분해서 넣어둬도 됩니다.
저는 yml을 디렉토리로 구분해서 사용해본 적이 없기 때문에, 이 부분에 대한 예시를 참고한 블로그에서 작성한 예시를 참고해서 적어보겠습니다.
[3 - application.yml 작성]
이제 이렇게 작성해준 yml들의 가장 상위의 계층이 되는 application.yml을 작성해주면 됩니다. 단, 이 application.yml의 위치를 나중에 변경하는 경우에는 이에 대한 경로를 또 바꿔주어야하기 때문에 추가 리소스가 소요됩니다. 그래서 고정적으로 사용할 위치를 정해서 application.yml을 작성해주면 됩니다.
저는 이렇게 yml(yaml) 파일의 디렉토리를 나누어 구성한 프로젝트를 진행해 본 경험이 없기 때문에, 이 부분에 대한 예시는 참고한 블로그의 예시를 통해 작성하겠습니다.
[디렉토리 구조]
project
├── src
│ ├── main
│ ├── java
│ ├── resources
│ │ ├── static
│ │ ├── templates
│ │ ├── yaml
│ │ │ ├── logging
│ │ │ │ └─ application-log.yml
│ │ │ ├── application-dev.yml
│ │ │ └─ application-local.yml
│ │ └─ application.yml
...
이 프로젝트에서는 logging 폴더 내에서 로그 기능을 담은 yml 파일을 따로 관리해주고 있다는 점을 알 수 있습니다.
각각의 yml 파일 내용을 살펴보면 다음과 같습니다.
[application-dev.yml]
spring:
config:
activate:
on-profile: dev
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul
username: {데이터베이스 관리자 이름}
password: {데이터베이스 비밀번호}
jpa:
hibernate:
ddl-auto: none
[application-local.yml]
spring:
config:
activate:
on-profile: local
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test
username: sa
password:
[application-log.yml]
spring:
config:
activate:
on-profile: log
jpa:
properties:
hibernate:
# show_sql: true # System.out 방식 로그
format_sql: true
highlight_sql: true
use_sql_comments: true
logging:
level:
org:
hibernate:
SQL: debug # logger 방식 로그
orm:
jdbc:
bind: trace
[application.yml]
spring:
config:
import:
- classpath:/yaml/application-prod.yml
- classpath:/yaml/application-dev.yml
- classpath:/yaml/application-local.yml
- classpath:/yaml/logging/application-log.yml
profiles:
group:
logging: dev, log
active: logging
# active: local
spring.config.import를 작성해주지 않아도 됩니다.spring.profiles.active에 원하는 profile을 넣어주면 됩니다.spring.profiles.group에spring.profiles.active에 해당하는 원하는 프로파일 이름을 적어주면 됩니다.[on-profile에 대하여]
application-{profile}.yml 형태로 작성된 경우를 보면 모두 아래와 같이 on-profile에 대한 내용을 담고 있습니다. (7차세미나에서 사용한 yml 파일에서도 동일하게 이 부분을 확인할 수 있습니다.)
spring:
config:
activate:
on-profile: log