
개발을 하다보면 자연스럽게 프로젝트에 따라서 설정을 다르게 하게 되는데
그 각종 설정을 변경할 수 있는 파일이 application.propertices이다.
그럼 application.yml은 뭐냐?
똑같이 설정을 변경하는 파일이지만 차이점이 있기 때문에 그 차이점을 알아보고자 한다.
.properticesspring.datasource.url=jdbc:h2:mem:togather
spring.datasource.username=sa
spring.datasource.password=null
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.default_batch_fetch_size=100
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=test@gmail.com
spring.mail.password=test
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
key = value의 형태로 되어있다..propertices 파일에서는 줄일 수가 없다. .ymlspring:
datasource:
url: jdbc:h2:mem:togather
username: sa
password:
driver-class-name: org.h2.Driver
h2:
console:
enabled: true
jpa:
defer-datasource-initialization: true
generate-ddl: true
hibernate:
ddl-auto: none
open-in-view: false
properties:
hibernate:
format_sql: true
show_sql: true
jdbc:
batch_size: 100
default_batch_fetch_size: 100
mail:
host: smtp.gmail.com
port: 587
username: test@gmail.com
password: test
properties:
mail:
smtp:
auth: true
starttls:
enable: true
| propertices | yml |
|---|---|
| java에서만 사용 | java, python, ruby에서도 사용 가능 |
| key=value로 비계층 구조 | 계층구조 |
| profile 마다 propertices 파일이 필요(2.4.0 버전부터는 하나의 파일에서 설정 가능) | 하나의 yml 파일에서 여러 profile에 대한 설정 가능 |
| 해당 파일에 설정된 값을 유형에 상관없이 문자열만을 가져옴 | 해당 파일에 설정된 값을 가져올 때 int, string등 유형에 맞게 가져올 수 있음 |
둘 다 사용해도 되지만, 나 같은 경우에는 yml 파일이 좀 더 가독성이 좋고 무엇보다 중복되는 코드를 줄일 수 있기에 yml 파일을 사용하는 편이다.
각자 프로젝트 컨벤션에 따르거나 편한대로 사용하면 될 것 같다.
참조 링크
https://www.baeldung.com/spring-boot-yaml-vs-properties
https://www.geeksforgeeks.org/difference-between-yaml-yml-and-properties-file-in-java-springboot/