Spring 설정파일 우선순위
Application.yaml 설정 파일 실행시 우선순위
매번 까먹어서 한번 재대로 정리해봄
default 셋업 아래에 on-profile 설정되어있는데 안먹어서 왜 안먹나 했더니 application.yaml이 우선순위가 높아서 default 옵션을 무시하고 다른 옵션이 들어가버려서 안되길래 도대체 어떤식으로 해결해야하는지 확인해봤다.
이경우에는 application.yaml에 profile이 있으면 됨 혹은 아예 파일이 따로 존재한다던지 여러가지 방법이 존재하니 참고
Application.yaml은 다음과 같은 우선순위를 가지게된다.
1. [Application properties](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files) packaged inside your jar (`application.properties` and YAML variants).
2. [Profile-specific application properties](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.profile-specific) packaged inside your jar (`application-{profile}.properties` and YAML variants).
3. [Application properties](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files) outside of your packaged jar (`application.properties` and YAML variants).
4. [Profile-specific application properties](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.profile-specific) outside of your packaged jar (`application-{profile}.properties` and YAML variants).
내부 Jar이 먼저 우선시 (application.yaml) → 내부 Jar의 Profile이 다음 순위 (application-profile.yaml) → Jar 밖의 Application-yaml → Jar 외부의 Profile이 마지막 순위
1. 구체적인 내부 로직은 다음과 같이 구성
Spring Boot는 애플리케이션이 시작될 때 다음 위치에서 application.properties 및 application.yaml 파일을 자동으로 우선순위를 매기게 됩니다.
- classpath 에서
- classpath 루트
- classpath /config 패키지
- 현재 디렉토리에서
- 현재 디렉토리
- 현재 디렉터리의 config/하위 디렉터리
- config/서브디렉토리의 바로 하위 디렉토리
2. Profile이 지정된 경우에는 다음처럼 우선순위 구성
Spring Boot는 application.yaml만 처리하는게 아닌, application-{profile}규칙을 가진 파일도 프로파일별 파일도 로드한다.
프로필별 속성은 표준 application.properties와 동일한 위치에서 로드되며, 프로필별 파일이 항상 비특정 파일보다 우선합니다. (비특정이라는 거는 아마 아예 yaml 파일인데, 이름이 완전 다른 케이스말하는듯? )
여러 프로필이 지정된 경우 last-wins
전략이 적용됩니다.
예를 들어, prod,live 프로파일이 spring.profiles.active 속성에 의해 지정된 경우, application-prod.properties의 값을 application-live.properties의 값으로 오버라이드(overriden)할 수 있습니다.
여기서 말하는 last-wins
전략은 다음과 같은 예로 이뤄집니다. last-wins
전략은 로케이션 그룹 레벨을 적용합니다.spring.config.location
에서 "," 인 케이스와 ";" 는 전략 적용방식이 다르니 아래 예시를 보면서 확인해봅시다.
활성화 된 파일의 순서는 다음 처럼 prod,live
되어있고, 파일이 다음처럼 위치해있다고 가정합시다.
/cfg
application-live.properties (or yaml)
/ext
application-live.properties
application-prod.properties
spring.config.location
가 classpath:/cfg/,classpath:/ext/
일 경우는 무조건 /cfg가 /ext 보다 우선순위가 높습니다.
/cfg/application-live.properties
/ext/application-prod.properties
/ext/application-live.properties
spring.config.location
가 classpath:/cfg/;classpath:/ext/
일 경우는 /cfg
와 /ext
가 동일 우선순위로 처리리 됩니다. 이 경우는 Prod가 높은 우선순위를 가집니다.
/ext/application-prod.properties
/cfg/application-live.properties
/ext/application-live.properties
활성 프로필이 설정되어 있지 않은 경우 사용되는 기본 프로필 세트(기본값은 default
)로 지정됨.
Core Features
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources including Java properties files, YAML files, environment variables, a
docs.spring.io
'Spring' 카테고리의 다른 글
테스트 코드를 짜고는 싶은데, 테스트 실패시 빌드 실패가 걱정된다면? (0) | 2023.09.02 |
---|---|
Spring boot build gradle에서 test task시 하면 좋은 것. (0) | 2023.09.02 |
Spring RestDocs를 통한 Swagger 페이지 구축기 - 1. 구성하기 (0) | 2022.07.24 |
Slack으로 Spring boot 에러 로깅 관제하기 (0) | 2022.07.18 |
스프링 롤백 테스트 하는 방식에 대한 생각 (1) | 2022.05.01 |