[Spring๐ฑ] application.properties ํ์ฉ
/ 7 min read
Table of Contents
Spring Boot์์ application.properties ํ๋กํ ๊ด๋ฆฌ
Spring Boot์์๋ application.properties
ํ์ผ์ ํตํด ๋ค์ํ ์ค์ ์ ๊ด๋ฆฌํ ์ ์๋ค. ๊ทธ๋ฌ๋ ๊ฐ๋ฐ ํ๊ฒฝ๊ณผ ๋ฐฐํฌ ํ๊ฒฝ์์ ํ์ํ ์ค์ ์ด ๋ค๋ฅผ ์ ์๊ธฐ ๋๋ฌธ์, ์ด๋ฅผ ํจ๊ณผ์ ์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํด ํ๋กํ(Profile)์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ค์ํ๋ค. ์ด๋ฒ ํฌ์คํ
์์๋ application.properties
์ ํ๋กํ์ ํ์ฉํ์ฌ ํ๊ฒฝ๋ณ ์ค์ ์ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ๊ณผ ์ด๋ฅผ ์ปดํฌ๋ํธ์์ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์ธํ ์์๋ณธ๋ค.
ํ๋กํ ๋๋๊ธฐ์ ํ์์ฑ๐ค
๊ฐ๋ฐ ๋จ๊ณ์ ๋ฐฐํฌ ๋จ๊ณ์์๋ ์๋ก ๋ค๋ฅธ ์ค์ ์ด ํ์ํ ์ ์๋ค. ์๋ฅผ ๋ค์ด, ๊ฐ๋ฐ ํ๊ฒฝ์์๋ ๋๋ฒ๊น ์ ์ํด ๋ก๊น ๋ ๋ฒจ์ ๋๊ฒ ์ค์ ํ๊ณ , ๋ฐฐํฌ ํ๊ฒฝ์์๋ ์ฑ๋ฅ์ ์ต์ ํํ๊ธฐ ์ํด ๋ก๊น ๋ ๋ฒจ์ ๋ฎ์ถ ์ ์๋ค. ์ด๋ฌํ ์ค์ ์ ํ๋์ ํ์ผ์ ๋ชจ๋ ํฌํจ์ํค๋ฉด ๊ด๋ฆฌ๊ฐ ์ด๋ ค์์ง๊ณ , ์ค์๋ก ์๋ชป๋ ์ค์ ์ ๋ฐฐํฌํ ์ํ์ด ์๋ค.
ํ๋กํ์ ๋๋์ด ๊ด๋ฆฌํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ฅ์ ์ ์ป์ ์ ์๋ค:
- ํ๊ฒฝ๋ณ ์ค์ ๊ด๋ฆฌ ์ฉ์ด: ๊ฐ๋ฐ, ํ ์คํธ, ๋ฐฐํฌ ๋ฑ ๊ฐ ํ๊ฒฝ์ ๋ง๋ ์ค์ ์ ๋ณ๋๋ก ๊ด๋ฆฌํ ์ ์๋ค.
- ๋ณด์ ๊ฐํ: ๋ฐฐํฌ ํ๊ฒฝ์์๋ ๋ฏผ๊ฐํ ์ ๋ณด๋ฅผ ์จ๊ธธ ์ ์๋ค.
- ์ ์ง๋ณด์์ฑ ํฅ์: ์ค์ ํ์ผ์ด ๋ช ํํ๊ฒ ๋ถ๋ฆฌ๋์ด ๊ด๋ฆฌ๊ฐ ์ฌ์์ง๋ค.
ํ๋กํ ์ค์ ๋ฐฉ๋ฒ ๐
Spring Boot์์๋ application.properties
๋๋ application.yml
ํ์ผ์ ๊ธฐ๋ณธ ์ค์ ํ์ผ๋ก ์ฌ์ฉํ๋ค. ์ฌ๊ธฐ์ ๊ฐ ํ๋กํ์ ๋ฐ๋ฅธ ์ค์ ํ์ผ์ ์ถ๊ฐํ์ฌ ํ๊ฒฝ๋ณ ์ค์ ์ ๊ด๋ฆฌํ ์ ์๋ค.
์ค์ ํ์ผ ์์ ๐
๊ธฐ๋ณธ ์ค์ ํ์ผ: application.properties
spring.application.name=learn-spring-bootlogging.level.org.springframework=debug
spring.profiles.active=dev
currency-service.url=https://qyinm.github.iocurrency-service.username=qyinmcurrency-service.key=defaultKey
๊ฐ๋ฐ ํ๋กํ ์ค์ ํ์ผ: application-dev.properties
spring.application.name=learn-spring-boot-devlogging.level.org.springframework=trace
currency-service.url=https://dev.qyinm.github.iocurrency-service.username=devUsercurrency-service.key=devKey
๋ฐฐํฌ ํ๋กํ ์ค์ ํ์ผ: application-prod.properties
spring.application.name=learn-spring-boot-prodlogging.level.org.springframework=error
currency-service.url=https://prod.qyinm.github.iocurrency-service.username=prodUsercurrency-service.key=prodKey
์ค๋ช ๐
- ๊ธฐ๋ณธ ์ค์ ํ์ผ (
application.properties
): ๋ชจ๋ ํ๋กํ์์ ๊ณตํต์ผ๋ก ์ฌ์ฉํ๋ ์ค์ ์ ์ ์ํ๋ค. - ํ๋กํ๋ณ ์ค์ ํ์ผ (
application-dev.properties
,application-prod.properties
): ํน์ ํ๋กํ์์๋ง ์ ์ฉ๋๋ ์ค์ ์ ์ ์ํ๋ค. ์๋ฅผ ๋ค์ด,dev
ํ๋กํ์์๋ ๊ฐ๋ฐ ํ๊ฒฝ์ ๋ง๋ ์ค์ ์,prod
ํ๋กํ์์๋ ๋ฐฐํฌ ํ๊ฒฝ์ ๋ง๋ ์ค์ ์ ์ ์ํ๋ค.
ํ๋กํ ํ์ฑํ ๋ฐฉ๋ฒ ๐ง
Spring Boot์์๋ spring.profiles.active
์์ฑ์ ์ฌ์ฉํ์ฌ ํ์ฑํํ ํ๋กํ์ ์ง์ ํ๋ค. ์ด ์์ฑ์ ๊ธฐ๋ณธ ์ค์ ํ์ผ์ ์ ์ํ๊ฑฐ๋, JVM ์ต์
์ ํตํด ์ง์ ํ ์ ์๋ค.
๋ฐฉ๋ฒ 1: ๊ธฐ๋ณธ ์ค์ ํ์ผ์์ ํ์ฑํ
spring.profiles.active=dev
๋ฐฉ๋ฒ 2: JVM ์ต์ ์ ํตํด ํ์ฑํ
์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ ๋ JVM ์ต์ ์ ์ถ๊ฐํ์ฌ ํ๋กํ์ ํ์ฑํํ ์ ์๋ค.
java -jar -Dspring.profiles.active=prod learn-spring-boot.jar
๋ฐฉ๋ฒ 3: ํ๊ฒฝ ๋ณ์๋ฅผ ํตํด ํ์ฑํ
ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ์ฌ ํ๋กํ์ ํ์ฑํํ ์๋ ์๋ค.
export SPRING_PROFILES_ACTIVE=prod
Component์์ ๊ฐ ๊ฐ์ ธ์ ์ฐ๊ธฐ ๐
Spring Boot์์๋ @ConfigurationProperties
์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ ๊ฐ์ ์ปดํฌ๋ํธ์ ์ฃผ์
ํ ์ ์๋ค. ์ด๋ฅผ ํตํด ํ๊ฒฝ๋ณ๋ก ๋ค๋ฅธ ์ค์ ๊ฐ์ ์์ฝ๊ฒ ๊ด๋ฆฌํ ์ ์๋ค.
์ฝ๋ ์์ ๐
CurrencyController.java
package com.hippoo.learnspringboot;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class CurrencyController {
@Autowired private CurrencyServiceConfiguration configuration;
@GetMapping("/currency-configuration") public CurrencyServiceConfiguration retrieveCurrencyConfiguration() { return configuration; }}
CurrencyServiceConfiguration.java
package com.hippoo.learnspringboot;
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "currency-service")@Componentpublic class CurrencyServiceConfiguration {
private String url; private String username; private String key;
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getKey() { return key; }
public void setKey(String key) { this.key = key; }}
์ค์ ํ์ผ ์์ ๐
application.properties
spring.application.name=learn-spring-bootlogging.level.org.springframework=debug
spring.profiles.active=dev
currency-service.url=https://qyinm.github.iocurrency-service.username=qyinmcurrency-service.key=defaultKey
application-dev.properties
spring.application.name=learn-spring-boot-devlogging.level.org.springframework=trace
currency-service.url=https://dev.qyinm.github.iocurrency-service.username=devUsercurrency-service.key=devKey
์ด๋ ๊ฒ ์ค์ ํ๋ฉด, ํ์ฑํ๋ ํ๋กํ์ ๋ฐ๋ผ ๋ค๋ฅธ ์ค์ ๊ฐ์ด CurrencyServiceConfiguration
์ปดํฌ๋ํธ์ ์ฃผ์
๋๋ค.
ํ๋กํ ์ฌ์ฉ ์ ์ฃผ์์ฌํญ โ ๏ธ
ํ๋กํ์ ์ฌ์ฉํ ๋ ๋ค์ ์ฌํญ์ ์ ์ํด์ผ ํ๋ค:
- ํ๋กํ ํ์ผ ๋ช
๋ช
๊ท์น ์ค์:
application-{profile}.properties
๋๋application-{profile}.yml
ํ์์ ๋ฐ๋ผ์ผ ํ๋ค. - ํ์ ํ๋กํผํฐ ํ์ธ: ํน์ ํ๋กํ์์๋ง ํ์ํ ํ๋กํผํฐ๊ฐ ์๋ค๋ฉด, ํด๋น ํ๋กํ ํ์ผ์ ๋๋ฝ๋์ง ์๋๋ก ์ฃผ์ํด์ผ ํ๋ค.
- ๋ณด์ ์ ๋ณด ๊ด๋ฆฌ: ๋ฏผ๊ฐํ ์ ๋ณด๋ ํ๋กํ๋ณ ์ค์ ํ์ผ์ ์ ์ฅํ์ง ์๊ณ , ํ๊ฒฝ ๋ณ์๋ ์ํฌ๋ฆฟ ๊ด๋ฆฌ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
- ํ๋กํ ํผ์ฉ ํผํ๊ธฐ: ํ ํ๊ฒฝ์ ์ฌ๋ฌ ํ๋กํ์ ๋์์ ํ์ฑํํ๋ฉด ์ค์ ์ถฉ๋์ด ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก, ๋ช ํํ ๊ตฌ๋ถ๋ ํ๋กํ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
๊ฒฐ๋ก ๐ฏ
Spring Boot์ ํ๋กํ ๊ธฐ๋ฅ์ ํ์ฉํ๋ฉด ๊ฐ๋ฐ, ํ
์คํธ, ๋ฐฐํฌ ๋ฑ ๋ค์ํ ํ๊ฒฝ์์ ํ์ํ ์ค์ ์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ค. ํ๋กํ์ ๋๋์ด ์ค์ ํ์ผ์ ๋ถ๋ฆฌํ๋ฉด ํ๋ก์ ํธ์ ์ ์ง๋ณด์์ฑ์ด ํฅ์๋๊ณ , ํ๊ฒฝ๋ณ๋ก ์ต์ ํ๋ ์ค์ ์ ์ ์ฉํ ์ ์๋ค. ๋ํ, @ConfigurationProperties
์ ๊ฐ์ ์ด๋
ธํ
์ด์
์ ํ์ฉํ๋ฉด ์ฝ๋์์ ์ค์ ๊ฐ์ ์ฝ๊ฒ ์ฃผ์
๋ฐ์ ์ฌ์ฉํ ์ ์๋ค.
ํ๋ก์ ํธ์ ๋ณต์ก์ฑ์ด ์ฆ๊ฐํจ์ ๋ฐ๋ผ ํ๋กํ์ ์ ์ ํ ํ์ฉํ์ฌ ํ๊ฒฝ๋ณ ์ค์ ์ ์ฒด๊ณ์ ์ผ๋ก ๊ด๋ฆฌํด๋ณด์. ์ด๋ฅผ ํตํด ๋์ฑ ๊ฒฌ๊ณ ํ๊ณ ์ ์ฐํ Spring Boot ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ถํ ์ ์์ ๊ฒ์ด๋ค.