application.propertices와 yml

한꼬북·2024년 5월 13일
1

spring

목록 보기
2/3
post-thumbnail

1. 그게 뭔데? 🙄

개발을 하다보면 자연스럽게 프로젝트에 따라서 설정을 다르게 하게 되는데
그 각종 설정을 변경할 수 있는 파일이 application.propertices이다.
그럼 application.yml은 뭐냐?
똑같이 설정을 변경하는 파일이지만 차이점이 있기 때문에 그 차이점을 알아보고자 한다.

2. 차이점

.propertices

spring.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 파일에서는 줄일 수가 없다.
  • java에서만 사용된다.

.yml

spring:
  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
  • 계층 구조로 이루어져있다.
  • 반복되는 접두어를 생략해 가독성이 좋은 편
  • 다른 언어에도 사용될 수 있다.(python 등등..)

정리

properticesyml
java에서만 사용java, python, ruby에서도 사용 가능
key=value로 비계층 구조계층구조
profile 마다 propertices 파일이 필요(2.4.0 버전부터는 하나의 파일에서 설정 가능)하나의 yml 파일에서 여러 profile에 대한 설정 가능
해당 파일에 설정된 값을 유형에 상관없이 문자열만을 가져옴해당 파일에 설정된 값을 가져올 때 int, string등 유형에 맞게 가져올 수 있음

결론

둘 다 사용해도 되지만, 나 같은 경우에는 yml 파일이 좀 더 가독성이 좋고 무엇보다 중복되는 코드를 줄일 수 있기에 yml 파일을 사용하는 편이다.
각자 프로젝트 컨벤션에 따르거나 편한대로 사용하면 될 것 같다.

참고

  • 한 프로젝트에서 둘 다 사용하는 것은 권장하지 않으며 만약 둘 다 사용할 경우 propertices의 우선 순위가 높아 같이 설정한 yml 파일이 적용되지 않을 수 있다고 한다.
  • 하나의 yml 파일에 여러 profile을 설정한 경우 작성 순서에 따라 우선 순위 영향을 끼침

참조 링크
https://www.baeldung.com/spring-boot-yaml-vs-properties
https://www.geeksforgeeks.org/difference-between-yaml-yml-and-properties-file-in-java-springboot/

profile
오히려 좋아, 자 가보자고!

0개의 댓글