Home > Spring > ๐Ÿƒ[Spring] `application.yml`๊ณผ `application.properties`์˜ ์ฐจ์ด์ .

๐Ÿƒ[Spring] `application.yml`๊ณผ `application.properties`์˜ ์ฐจ์ด์ .
Spring Framework

๐Ÿƒ[Spring] application.yml๊ณผ application.properties์˜ ์ฐจ์ด์ .

Java ๋ฐฑ์—”๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ application.yml๊ณผ application.properties๋Š” ๋ชจ๋‘ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ค์ • ์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ์„ฑ ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.

์ด ๋‘ ํŒŒ์ผ์€ Spring Boot์™€ ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ™˜๊ฒฝ ์„ค์ •, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ, ํฌํŠธ ๋ฒˆํ˜ธ, ๋ณด์•ˆ ์„ค์ • ๋“ฑ์„ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋‘ ํŒŒ์ผ์€ ๊ธฐ๋Šฅ์ ์œผ๋กœ ๋น„์Šทํ•˜์ง€๋งŒ ํ˜•์‹๊ณผ ๊ฐ€๋…์„ฑ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ ์ฐจ์ด์ .

1. ํŒŒ์ผ ํ˜•์‹.

  • application.properties
    • ํ‚ค-๊ฐ’ ์Œ ํ˜•์‹์˜ ๊ตฌ์„ฑ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ๊ฐ ์„ค์ •์€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ, key=value ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค.
  • application.yml
    • YAML ํ˜•์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • YAML์€ ๊ณ„์ธต์  ๊ตฌ์กฐ์™€ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ํ†ตํ•ด ์„ค์ •์„ ์ •์˜ํ•˜๋ฉฐ, JSON๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ

  • application.properties ์˜ˆ์‹œ
    server.port=8080
    spring.dataspurce.url=jdbc:mysql://localhost:3306/mydb
    spring.datasource.username=root
    spring.datasource.password=secret
    logging.level.org.springframework=DEBUG
    
  • application.yml ์˜ˆ์‹œ
    server:
      port: 8080
    spring:
      datasource:
          url: jdbc:mysql://localhost:3306/mydb
          username: root
          password: secret
    logging:
      level:
          org.springframework: DEBUG
    

2. ๊ณ„์ธต ๊ตฌ์กฐ ํ‘œํ˜„.

  • application.properties
    • ๊ฐ ์„ค์ • ํ•ญ๋ชฉ์€ ์  ํ‘œ๊ธฐ๋ฒ•(dot natation) ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
      • ์˜ˆ๋ฅผ ๋“ค์–ด, spring.datasource.url ์ฒ˜๋Ÿผ ์ ์„ ์‚ฌ์šฉํ•ด ์ค‘์ฒฉ๋œ ์†์„ฑ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • application.yml
    • YAML์€ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ํ†ตํ•ด ๊ณ„์ธต์  ๊ตฌ์กฐ๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์  ํ‘œ๊ธฐ๋ฒ• ๋Œ€์‹  ๋“ค์—ฌ์“ฐ๊ธฐ๋กœ ์ค‘์ฒฉ๋œ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

3. ๊ฐ€๋…์„ฑ

  • application.properties
    • ๋ชจ๋“  ์„ค์ •์ด ํ•œ ์ค„์— ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ํ‘œ์‹œ๋˜๋ฏ€๋กœ ๊ฐ„๋‹จํ•œ ์„ค์ •์—์„œ๋Š” ์ฝ๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.
    • ๊ทธ๋Ÿฌ๋‚˜ ๊ณ„์ธต์  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•ด์•ผ ํ•  ๋•Œ๋Š” ์  ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ์„ค์ •์ด ๋งŽ์•„์งˆ ์ˆ˜๋ก ์ฝ๊ธฐ ์–ด๋ ค์›Œ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • application.yml
    • YAML์€ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ๋•Œ๋ฌธ์• , ๋ณต์žกํ•œ ์„ค์ •์„ ๋” ๊ฐ€๋…์„ฑ ์žˆ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์„ค์ •์ด ๋งŽ๊ฑฐ๋‚˜ ์ค‘์ฒฉ๋œ ๊ฒฝ์šฐ์—๋„ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. ๋ฐ์ดํ„ฐ ํ‘œํ˜„์˜ ์œ ์—ฐ์„ฑ.

  • application.properties
    • ๋‹จ์ˆœํžˆ ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ ํ‘œํ˜„์ด ์ œํ•œ๋ฉ๋‹ˆ๋‹ค.
    • ๋ฐฐ์—ด์ด๋‚˜ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ์  ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • application.yml
    • YAML์€ ๋ฐฐ์—ด, ๊ฐ์ฒด, ์ค‘์ฒฉ๋œ ๊ตฌ์กฐ๋ฅผ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ ๋” ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค.

๋ฐฐ์—ด ํ‘œํ˜„ ์˜ˆ์‹œ.

  • application.properties ์—์„œ ๋ฐฐ์—ด์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•.
    mylist[0]=item1
    mylist[1]=item2
    mylist[2]=item3
    
  • application.yml ์—์„œ ๋ฐฐ์—ด์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•.
    ```bash
    mylist:
    • item1
    • item2
    • item3
      ```

5. ์ฃผ์„.

  • application.properties
    • ์ฃผ์„์€ # ๊ธฐํ˜ธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
    • ์ฃผ์„์€ ํ•œ ์ค„์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • application.yml
    • ์ฃผ์„๋„ # ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ์ฃผ์„์„ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹์€ application.properties์™€ ๋™์ผํ•˜์ง€๋งŒ, YAML ํ˜•์‹์—์„œ๋Š” ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์นœ ์ฃผ์„์„ ์ถ”๊ฐ€ํ•˜๊ธฐ์— ๋” ์ž์—ฐ์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค.

6. ์‚ฌ์šฉ ์šฉ๋„.

  • application.properties
    • ๋‹จ์ˆœํ•œ ์„ค์ •์„ ์ •์˜ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ์†์„ฑ ์ˆ˜๊ฐ€ ์ ๊ณ  ๊ณ„์ธต์  ๊ตฌ์กฐ๊ฐ€ ๋งŽ์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋” ์ง๊ด€์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • application.yml
    • ๋ณต์žกํ•œ ์„ค์ •์„ ์ •์˜ํ•  ๋•Œ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
    • YAML์€ ๋ฐ์ดํ„ฐ์˜ ๊ณ„์ธต์  ๊ตฌ์กฐ๋ฅผ ์‰ฝ๊ฒŒ ํ‘œํ˜„ ํ•  ์ˆ˜ ์žˆ์–ด, ์ค‘์ฒฉ๋œ ์„ค์ •์ด๋‚˜ ๋‹ค์ˆ˜์˜ ์„ค์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ์„ ํƒ ๊ธฐ์ค€

  • ์ž‘์€ ํ”„๋กœ์ ํŠธ๋‚˜ ๋‹จ์ˆœํ•œ ์„ค์ •์—๋Š” application.properties๊ฐ€ ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์  ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๊ฐ„๋‹จํžˆ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ง๊ด€์ ์ด๊ณ  ๋น ๋ฅด๊ฒŒ ์„ค์ •์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ณต์žกํ•œ ํ”„๋กœ์ ํŠธ๋‚˜ ๋‹ค์ค‘์ ์ธ ์„ค์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ํŠนํžˆ ์„ค์ • ๋กœ๊น… ๋ ˆ๋ฒจ ์„ค์ •, ๋‹ค์ค‘ ํ™˜๊ฒฝ ๊ด€๋ฆฌ ๋“ฑ์˜ ๋ณต์žกํ•œ ๊ตฌ์„ฑ์ด ์š”๊ตฌ๋  ๋•Œ๋Š” application.yml์ด ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
    • YAML์˜ ๊ตฌ์กฐ์  ํ‘œํ˜„ ๋•๋ถ„์— ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ ์š”์•ฝ.

  • application.properties
    • ๋‹จ์ˆœํ•œ ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์„ค์ • ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.
    • ์  ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด ๊ณ„์ธต์  ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•˜๋ฉฐ, ๋‹จ์ˆœํ•œ ์„ค์ •์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • application.yml
    • YAML ํ˜•์‹์˜ ์„ค์ • ํŒŒ์ผ๋กœ, ๋“ค์—ฌ์“ฐ๊ธฐ์™€ ๊ณ„์ธต์  ๊ตฌ์กฐ๋ฅผ ํ†ตํ•ด ๋ณต์žกํ•œ ์„ค์ •์„ ๋ณด๋‹ค ์ง๊ด€์ ์ด๊ณ  ๊ฐ€๋…์„ฑ ์žˆ๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๋ณต์žกํ•œ ํ”„๋กœ์ ํŠธ์—์„œ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ, ๋‘˜ ๋‹ค ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์„ค์ •์˜ ๋ณต์žก๋„์™€ ๊ฐ€๋…์„ฑ ์š”๊ตฌ์— ๋”ฐ๋ผ properties์™€ yml ์ค‘ ์ ํ•ฉํ•œ ํ˜•์‹์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.