-
OS환경변수
-
자바 시스템 속성
-
CommandLine 인수 ( + 옵션 인수 )
- public static void main(String[] args)
-
외부파일
- jar 외부에 존재, 로딩시점에 외부파일을 읽어서 사용
ex) application.properties
-
내부 파일 분리
- 개발용 설정파일, 운영용 설정파일 을 함께 포함해서 관리
- Profile로 구분 ( Profile : 외부 설정값 )
- CommandLine 옵션인수
ex) —spring.profiles.active=dev
자바시스템 속성
ex) java -Dspring.profiles.active=dev -jar XXX.jar
-
내부 파일 통합
- spring.config.activate.on-profile=XXX 로 데이터를 구분
- application.properties
spring.config.activate.on-profile=dev
url=devdb
#---
spring.config.activate.on-profile=prod
url=proddb
-
우선순위
url=defaultdb
# , 로 구분하면 List Class로 변환
options=A,B
# ms로 끝나면 Duration Class로 변환
timeout=200ms
#---
spring.config.activate.on-profile=dev
url=devdb
#---
spring.config.activate.on-profile=prod
url=proddb
- 설정파일을 위에서 아래로 읽어가며 값을 설정
- 프로필 여러개 설정가능
ex) —spring.profiles.active=dev,prod
-
사용
flowchart LR
Client --> Environment
Environment --> PropertySourceA
Environment --> PropertySourceB
Environment --> PropertySourceC
Environment --> PropertySourceD
-
PropertySource
- CommandLinePropertySource
- SystemEnvironmentPropertySource
- …
-
Environment
- PropertySource들을 조회해 결과 반환
- 우선순위 존재
- 적용범위가 작은 것이 우선권
- 더 유연한 것이 우선권을 가짐 ( 파일보다 자바 시스템 속성이 우선권 ) : ???
- Bean으로 존재 (DI 가능)
ex) private final Environment env;
env.getProperty(key)
-
@Value - 값 주입
my.file=file
- 생성자 선언, 멤버변수 선언 에서 사용가능
- 기본값 설정 가능 ex) my.file:file2
-
@ConfigurationProperties - Type-Safe Configuration Proprerties
- @ConfigurationProperties(”XX”) : XX 는 Property Key의 prefix 값
- 외부설정의 묶음 정보를 객체로 변환 ( ⇒ 외부 설정을 자바코드로 관리 )
- 기본 주입 방식은 Java Bean Property 방식 ( Getter, Setter 필요 )
- Setter를 가지고 있어 변경 위험
Setter를 제거하고 생성자를 사용하면 위험 방지
- @EnableConfigurationProperties(XXX.class) : XXX 는 주입받을 객체
- properties에서 KebapCase(a-b)을 CamelCase(aB)로 변환
- @ConfigurationPropertiesScan(”패키지명”) 을 사용하면 @EnableConfigurationProperties(XXX.class)를 생략가능
- @DefaultValue를 사용하여 기본값 사용가능
- Java Bean Validation 사용가능 ( Java Bean Validation)
-
@ConfigurationProperties - 생성자 사용
- 주입받을 객체에 @Setter 제거 후 생성자 추가
- 이전에는 @ConstructBinding이 필요했지만 생략
생성자가 여러개 일경우 사용할 생성자에 해당 Annotation 사용 ( @Autowired랑 동일한듯? )
-
YAML ( 우선순위 : .properties > .yml )
YAML
-
@Profile ( = Condition )
- 기본 프로필은 default
- @Conditional을 포함 ( 조건이 맞으면 true )