spring Environment란?
스프링에서 제공화는 환경설정에 대한 인터페이스
설정 값의 변경은 불가능하고 읽을수만 있다.
환경변수를 적을 파일을 만들고 다양한 방법으로 끌어와서 사용할 수 있다.
@PropertySource("application.properties") 등록 후
ConfigurableEnvironment environment = applicationContext.getEnvironment(); String version = environment.getProperty("kdt.version"); Integer property = environment.getProperty("kdt.minimum-order-amount", Integer.class);
@Value를 통해 필드에서도 값을 가져올 수 있다.
많약 일치하는 값이 없을경우 default 값을 지정가능.
@Value는 정적값을 지정할 수 도 있다.@Value("${version}:v1.1.1.1.1.1") private String version;
@ConfigurationProperties(prefix = "kdt")를 통해 yaml파일의 값을 클래스의 필드들과 매핑시킬 수도 있다.
@Profile
@Profile로 각 클래스의 Profile을 지정할 수 있다.
new AnnotationConfigApplicationContext();
으로 빈 컨테이너를 생성한 후
applicationContext.register(AppConfiguration.class);
로 지정할 수 있다.
이후 enviroment를 꺼내서 profile값을 정해주는 것도 가능.
ConfigurableEnvironment environment = applicationContext.getEnvironment();
environment.setActiveProfiles("local");
applicationContext.refresh();