์ธ๋ถ ๊ตฌ์ฑ (External Configuration)
๐ ๊ฐ์
- ์ค์ ์ ์ฝ๋ ๋ฐ์์ ๊ด๋ฆฌํ๋ค. โ ํ๊ฒฝ(DEV/PROD)ยทSecret ํค ๋ถ๋ฆฌ, ๋ฌด์ค๋จ ๋ณ๊ฒฝ
- Spring Boot๋ ๋ค์ํ ์์ค(CLI ์ธ์, ํ๊ฒฝ๋ณ์, ํ์ผ ๋ฑ)๋ฅผ ์ฐ์ ์์์ ๋ฐ๋ผ ๋ณํฉํ๊ณ ,
@Value
/@ConfigurationProperties
๋ก ์ฃผ์ ํ๋ค. - ๋จ์ผ ๊ฐ์
@Value
, ์ฌ๋ฌ ๊ฐ/๊ทธ๋ฃน์@ConfigurationProperties
๋ฅผ ๊ถ์ฅ.
๐ ๋ํ ๊ตฌ์ฑ ํ์ผ
application.properties
/application.yml
(ํด๋์คํจ์ค ๋ฃจํธ ๋๋/config
ํด๋)- ํ๋กํ๋ณ:
application-dev.yml
,application-prod.yml
๋ฑ
# application.yml
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: app
๐ ์ค์ ์ฐ์ ์์ (Spring Boot ๊ธฐ์ค)
- Command line arguments (
--server.port=9000
) - OS ํ๊ฒฝ๋ณ์ / SPRING_APPLICATION_JSON
application-{profile}.yml
(์ธ๋ถ/config
โ ํด๋์คํจ์ค)application.yml
(์ธ๋ถ/config
โ ํด๋์คํจ์ค)@PropertySource
,@ConfigurationProperties
์ธ๋ถ
/config
ํด๋๊ฐ ํด๋์คํจ์ค๋ณด๋ค ์ฐ์ ์์๊ฐ ๋๋ค.
๐งฐ ์ ์ฉ ๋ฐฉ์
(A) ๋จ์ผ ๊ฐ : @Value
@Value("${spring.datasource.url}")
private String dbUrl;
(B) ๋ฌถ์ ๋ฐ์ธ๋ฉ : @ConfigurationProperties
(๊ถ์ฅ)
// application.yml
app:
storage:
endpoint: https://s3.example.com
bucket: media
@ConfigurationProperties(prefix = "app.storage")
public class StorageProps {
private String endpoint;
private String bucket;
// getters/setters
}
โ ์ด ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ ๋ฌถ์ด์ ์ฃผ์ ํ ๋ ์ ์ฉ
๐ ํ๊ฒฝ๋ณ ์ค์
# application.properties
spring.profiles.active=dev
# application-dev.yml
logging:
level:
root: DEBUG
# application-prod.yml
logging:
level:
root: INFO
๋ฐํ์์
--spring.profiles.active=prod
๋ก๋ ์ ํ ๊ฐ๋ฅ. ํน์ ๋น์ ํ๊ฒฝ๋ณ๋ก ๋ฐ๊พธ๋ ค๋ฉด@Profile("dev|prod")
์ฌ์ฉ.
๐ ๊ด๋ จ
- auto-configuration - ์กฐ๊ฑด(
@Conditional*
)๊ณผ ์์ฑ์ผ๋ก ๊ธฐ๋ณธ ๋น ์๋ ๋ฑ๋ก - DI - ๊ตฌ์ฑ๋ ๊ฐ์ ๋น์ผ๋ก ์ฃผ์ ํด ๊ฒฐํฉ๋ โ
- IoC - ์ปจํ ์ด๋๊ฐ ์์ฑ/์ฃผ์ /์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌํ๋ ๊ธฐ๋ฐ ์๋ฆฌ