일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- SpringBoot
- 네이버웹마스터도구
- 금요웹툰
- spring banner
- 이번생도잘부탁해
- 네이버웹툰추천
- 금요웹툰추천
- 완결웹툰추천
- vmware full screen
- 네이버웹툰
- 모랑지
- 소녀의 세계
- 복학생정순이
- 월요웹툰
- 스프링부트
- 걸크러시웹툰
- 한끼룩
- vmware timesync
- spring boot
- 메데이아
- 화요웹툰
- 교환일기
- 일요웹툰
- 하네되
- 오늘도사랑스럽개
- 네이버서치어드바이저
- 웹툰추천
- 한짜장
- 월요웹툰추천
- 하루만네가되고싶어
조아유 창고
[Spring Boot 2.3.1] Ref. 요약(7) Spring Boot Features(4) 본문
2. Externalized Configuration
Spring Boot는 설정한 것을 외부화해서 다른 환경에서 같은 어플리케이션 코드로 작업할 수 있습니다. property file, YAML file, 환경변수, command-line 변수 등을 사용하면 됩니다. 프로퍼티 값은 @Value를 사용해 beans에 직접 주입 할수도 있고 스프링 환경 추상화를 통해 접근 가능 하거나 @ConfigurationProperties를 통해 구조화 된 객체에 바운드 될 수도 있습니다.
Spring Boot는 값의 오버라이딩 허용 하도록 설계된 특정 PropertySource 순서를 사용합니다.
properties는 아래와 같은 순서로 고려됩니다. 번역할 거리가 별로 없어 원문 그대로 복사해왔어요.
-
Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.
-
@TestPropertySource annotations on your tests.
-
properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
-
Command line arguments.
-
Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
-
ServletConfig init parameters.
-
ServletContext init parameters.
-
JNDI attributes from java:comp/env.
-
Java System properties (System.getProperties()).
-
OS environment variables.
-
A RandomValuePropertySource that has properties only in random.*.
-
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
-
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
-
Application properties outside of your packaged jar (application.properties and YAML variants).
-
Application properties packaged inside your jar (application.properties and YAML variants).
-
@PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
-
Default properties (specified by setting SpringApplication.setDefaultProperties).
예제로는, name 프로퍼티 사용하는 @Component 입니다.
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
@Component
public class MyBean {
@Value("${name}")
private String name; // ...
}
디폴트 프로퍼티 값은 application.properties 에서 정의할 수 있어요. 새로운 환경에서 실행될때 이 파일은 이름을 오버라이드하여 jar 외부로 제공될 수 있습니다. 일회성 테스트에서는 구체적인 커맨드 라인 전환으로 실행시킬 수 있습니다.
Spring Boot는 컨피그 파일을 로딩할 때 와일드카드 위치를 지원합니다. 디폴트로 jar 외부의 config/*/ 와일드카드 위치가 지원됩니다. 와일드카드 위치는 spring.config.additional-location and spring.config.location 를 구체화할때도 지원됩니다.
와일드카드 위치는 특히 컨피그 프로퍼티의 다양한 소스가 있을 때 Kubernetes 환경에 유용합니다. 예를 들어, Redis config와 MySQL 컨피그를 가지고 있다면, 두 가지 컨피그를 따로 유지하면서 앱을 바인드할 수 있는 application.properties에서 둘 다 존재해야 합니다. 결과적으로 application.properties 파일들은 각각 다른 곳에 위치하게 될 겁니다. config/*/ 이런 식으로 표기하면 두 파일 모두 진행 될 수 있게 됩니다.
와일드카드 위치는 오직 하나의 빈칸과 /를 포함할 수 있습니다.
SPRING_APPLICATION_JSON 프로퍼티는 환경변수와 함께 커맨드라인에서 제공됩니다.
$ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar
시스템 프로퍼티에서 spring.application.json 같은 JSON 형태로도 사용할 수 있습니다.
$ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar
$ java -jar myapp.jar --spring.application.json='{"name":"test"}'
2.1. Configuring Random Values
RandomValuePropertySource 는 랜덤 값을 넣을 때 유용합니다. integer, long, uuid, string 등을 만들 수 있습니다.
random.int* 구문은 OPEN value (,max) CLOSE입니다. OPEN, CLOSE는 문자이고, value, max는 integer입니다. max가 제공되면 value는 최소값 이고 max는 최대값입니다.
2.2. Accessing Command Line Properties
디폴트로 SpringApplication은 커맨드라인 옵션 인수(--로 시작하는)를 프로퍼티로 변환하고 Spring 환경에 추가합니다. 이전에 언급했듯 커맨드 라인 프로퍼티는 항상 다른 속성보다 우선시 됩니다.
커맨드라인 프로퍼티들이 환경에 추가되지 않길 원하면 SpringApplication.setAddCommandLineProperties(false)를 사용해 비활성화시킬 수도 있습니다.
2.3. Application Property Files
SpringApplication은 application.properties로 부터 프로퍼티를 받아오고 그들을 Spring 환경에 추가합니다.
-
A /config subdirectory of the current directory
-
The current directory
-
A classpath /config package
-
The classpath root
리스트는 우선 순위에 따라 정렬됩니다 (높은 위치에 정의 된 속성은 낮은 위치에 정의 된 속성보다 우선시 됩니다).
application.properties는 파일명을 spring.config.name 환경 변수에 구체화해서 변경할 수 있습니다. spring.config.location 속성을 사용하면 명시적 위치를 참조할 수 있습니다. 구체화는 아래와 같이 설정하면 됩니다.
$ java -jar myproject.jar --spring.config.name=myproject
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
spring.config.location이 디렉토리를 가지고 있으면 /로 끝나야 합니다. 여기에 구체화된 파일은 profile-specific variants 를 위한 지원 없이 사용하던 대로 하면 되고 프로파일 별로 재정의됩니다.
컨피그 위치는 역순으로 검색됩니다. 디폴트로 설정된 위치는 classpath:/, classpath:/config/, file:./, file:./config/*/ ,file:./config/ 입니다. 결과 검색 순서는 다음과 같습니다.
-
file:./config/
-
file:./config/*/
-
file:./
-
classpath:/config/
-
classpath:/
커스텀 컨피그 위치가 spring.config.location로 설정될 때, 위치들은 디폴트 위치를 대신합니다. 예를 들어 spring.config.location이 classpath:/custom-config/,file:./custom-config/로 설정되면 검색순서는 다음과 같습니다.
-
file:./custom-config/
-
classpath:custom-config/
custom config 위치가 spring.config.additional-location를 사용해 설정되면 디폴트에 추가되어 사용됩니다. 추가 위치는 디폴트 위치 전에 검색됩니다. 예를 들어, 추가 위치가 classpath:/custom-config/,file:./custom-config/로 설정되면 검색 순서는 다음과 같습니다.
-
file:./custom-config/
-
classpath:custom-config/
-
file:./config/
-
file:./config/*/
-
file:./
-
classpath:/config/
-
classpath:/
검색 순서는 디폴트 값을 구체화 시키고 선택적으로 다른 곳에 재정의할 수도 있습니다. application.properties에 있는 디폴트 값을 디폴트 위치 중 한 곳에 제공할 수 있습니다. 이런 디폴트 값은 커스텀 위치 중 한곳에 위치한 다른 파일과 함께 런타임 중에 재정의 될 수 있습니다.
2.4. Profile-specific Properties
application.properties 파일 외에 profile-specific 프로퍼티는 application-{profile}.properties와 같은 이름 규칙을 사용해 정의됩니다. 환경은 비활성화 프로파일이 설정되어 있다면 사용되는 디폴트 프로파일을 가지고 있습니다. 즉, 어떤 프로파일도 활성화 되지 않았다면 그때 application-default.properties에 있는 프로퍼티가 로드됩니다.
Profile-specific properties 는 profile-specific이 패키지 jar 안에 있든 밖에 있든 non-specific을 항상 재정의하며 기본 application.properties와 같은 위치에서 로드됩니다.
여러 프로파일이 구체화된다면 마지막 승리 정략이 적용됩니다. 예를 들어, spring.profiles.active에 의해 구체화된 프로파일이 SpringApplicaition API를 통해 설정된 후 추가되므로 우선시 됩니다.
'IT tips' 카테고리의 다른 글
[Spring Boot 2.3.1] Ref. 요약(9) Spring Boot Features(6) (0) | 2020.07.10 |
---|---|
[Spring Boot 2.3.1] Ref. 요약(8) Spring Boot Features(5) (0) | 2020.07.07 |
[Spring Boot 2.3.1] Ref. 요약(6) Spring Boot Features(3) (0) | 2020.06.26 |
[Spring Boot 2.3.1] Ref. 요약(5) Spring Boot Features(2) (0) | 2020.06.24 |
VMware issues (0) | 2020.06.22 |