1. Command Line Properties
항목 | 설명 |
정의 |
SpringApplication은 명령행 인자(Command Line Arguments)를 PropertySource로 변환해 Environment에 추가함
|
우선순위 |
매우 높음 → .properties, .yml, 환경 변수보다 우선 적용됨
|
오버라이드 여부 |
명령행 인자가 동일 키의 다른 설정들을 오버라이드함
|
비활성화 방법 |
SpringApplication.setAddCommandLineProperties(false)를 호출하면 Environment에 추가되지 않음
|
$ java -jar app.jar --server.port=8085
2. JSON Application Properties
항목 | 설명 |
정의 |
JSON 문자열로 애플리케이션 설정을 주입하는 방식
|
사용 키 |
spring.application.json (또는 환경변수 SPRING_APPLICATION_JSON)
|
특징 |
일반적인 key를 사용할 수 없는 경우, JSON 구조로 인코딩하여 우회 가능
|
$ SPRING_APPLICATION_JSON='{"my":{"name":"test"}}' java -jar myapp.jar
$ java -Dspring.application.json='{"my":{"name":"test"}}' -jar myapp.jar
$ java -jar myapp.jar --spring.application.json='{"my":{"name":"test"}}'
3. External Application Properties
- Spring Boot는 자동적으로 config data를 찾아 로드합니다.
- config data를 찾기위해 기본적으로 설정된 클래스 패스와 디렉토리가 있습니다.
기본 검색 경로
구분 | 경로 |
classpath |
root classpath, /config 하위 classpath
|
directory |
현재 디렉토리, config/ 하위 디렉토리, config/의 하위 디렉토리들
|
주요 프로퍼티
Key | 설명 |
spring.config.name | config 파일 이름 설정 (기본: application) |
spring.config.location |
config 파일의 기본 위치 지정
여러 개일 경우 세미콜론(;)으로 구분 |
spring.config.additional-location |
기본 위치 외에 추가적인 설정 파일 경로 지정 (기존 설정을 오버라이딩)
|
spring.config.import |
외부 config 파일 또는 시스템에서 import (ZooKeeper, Archaius 등)
import 커스터마이징 객체를 지원하는 클래스를 제공합니다. (ConfigDataLocationResolver, ConfigDataLoader) |
spring.config.name=myapp
spring.config.location=classpath:/default-config/;file:/etc/myapp/
Optional Locations
키워드 | 설명 |
optional: |
해당 config 파일이 없어도 에러 없이 무시됨
spring.config.location의 prefix로 사용됨 |
* (Wildcard) |
하위 파일 전체를 알파벳 순으로 로드
로컬 파일 시스템의 외부 디렉토리를 지정할 때만 사용할 수 있습니다. |
$ java -jar myproject.jar --spring.config.location=\
optional:classpath:/default.properties,\
optional:classpath:/override.properties
Profile Specific Files
Key | 설명 |
spring.profiles.active |
활성화할 profile 설정 (comma-separated list)
|
Profile config 파일 |
application-{profile}.yml / .properties
|
기본 + profile 설정 |
기본 설정은 default 값, profile 설정은 override
|
.
Using Configuration Trees
"configtree"
- 클라우드 플랫폼 내에 config data가 있을 경우 참조를 가리키는 키워드입니다.
- 클라우드 플랫폼에서 Spring Boot 애플리케이션을 운영할 때 사용됩니다.
/etc/config/
└── myapp/
├── username
└── password
# application.yml
spring.config.import=optional:configtree:/etc/config/
- spring boot에서 클라우드 플랫폼의 config data를 접근하여 읽고 propertysource로 등록합니다.
- myapp.username, myapp.password로 접근합니다.
Property Placeholders
- config data를 읽어들일 때, 값 참조 및 디폴트 값 설정을 지원하는 문법입니다.
문법 | 설명 |
${key} | key 값 참조 |
${key:default} |
key가 없을 경우 default 사용
|
app.name=MyApp
app.description=${app.name} is a Spring Boot application written by ${username:Unknown}
- relaxed binding 전략이 적용됩니다.
${demo.item-price}
-> demo.item-price
-> demo.itemPric
-> DEMO_ITEMPRICE
Activation Properties
- 특정 환경에서 동작하는 조건을 명시하는 property key 입니다.
키 | 설명 |
spring.config.activate.on-cloud-platform |
클라우드 플랫폼 조건 (예: kubernetes)
|
spring.config.activate.on-profile |
특정 프로파일 조건 (예: prod, staging)
|
myprop=always-set
#---
spring.config.activate.on-cloud-platform=kubernetes
spring.config.activate.on-profile=prod | staging
myotherprop=sometimes-set
# 운영 환경
java -jar myapp.jar --spring.profiles.active=prod
Working With Multi-Document Files
- Spring Boot는 하나의 config data 파일에 독립적으로 여러 profile의 config data를 작성할 수 있습니다.
- 활성화 조건을 기입할 수 있으며, 조건에 일치할 경우에만 적용됩니다
구분자 | 파일 형식 |
--- | .yaml |
#---, !--- | .properties |
spring:
application:
name: "MyApp"
---
spring:
application:
name: "MyCloudApp"
config:
activate:
on-cloud-platform: "kubernetes"
spring.application.name=MyApp
#---
spring.application.name=MyCloudApp
spring.config.activate.on-cloud-platform=kubernetes
4. 우선순위
우선순위 | PropertySource 종류 | 설명 |
1 | Command Line Arguments |
ex. --server.port=9090
|
2 | SPRING_APPLICATION_JSON |
ex. -Dspring.application.json='{"key":"value"}'
|
3 | ServletConfig init parameters | 웹 컨테이너 설정 |
4 | ServletContext init parameters | 웹 컨텍스트 설정 |
5 | JNDI attributes (java:comp/env) |
자바 네이밍 디렉터리
|
6 | Java System properties (System.getProperties()) | JVM 속성 |
7 | OS Environment Variables | 시스템 환경 변수 |
8 | RandomValuePropertySource | random.* 값 제공 |
9 | Config Data (application.properties/yaml 등) |
아래 Config Data 순서 참조
|
10 | @PropertySource |
Java 설정 클래스에 명시한 외부 설정
|
11 | Default properties (setDefaultProperties()) |
코드에서 설정한 기본값
|
12 | Test 관련 프로퍼티 (@TestPropertySource, @DynamicPropertySource) |
테스트에서 우선 적용됨
|
13 | Devtools 설정 ($HOME/.config/spring-boot) |
개발 시 추가 설정 위치
|
- 동일한 프로퍼티 키가 여러 PropertySource에 존재할 경우, 늦게 등장하는 순서의 PropertySource 값으로 오버라이드됩니다.
Config Data 우선 순위
우선순위 | 위치 | 설명 |
1 | application-{profile}.yml (외부) |
jar 외부의 profile-specific 설정
|
2 | application.yml (외부) |
jar 외부의 기본 설정
|
3 | application-{profile}.yml (내부) |
jar 내부의 profile-specific 설정
|
4 | application.yml (내부) |
jar 내부의 기본 설정
|
5. 주입 방식
설정 방법 | 설명 |
@Value 어노테이션 | 개별 값 주입 |
@ConfigurationProperties | 설정 객체로 바인딩 (대규모 설정에 유리) |
참고
'Spring > Spring Boot' 카테고리의 다른 글
[Spring Boot] 2-6. Core Features: JSON (0) | 2023.10.09 |
---|---|
[Spring Boot] 2-3. Core Features: Profile (0) | 2023.10.09 |
[Spring Boot] 2-1. Core Features: SpringApplication 클래스 (0) | 2023.10.08 |
[Spring Boot] 1-2. Spring Boot 사용하기 (1) | 2023.10.07 |
[Spring Boot] 1-1. Spring Boot 사용하기 (0) | 2023.10.07 |