SpringBoot에서 외부 yml 객체 맵핑하여 사용하기
Jul 21, 2017 · 7 min read
1. Bean 등록
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;@Component
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("external-config.yml"));
propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}
}/**
* 응용예제 : spring.profiles별 적용할때...
*/@Component
public class Config {
@Profile("local")
public static class LocalConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertiesLocal() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("/local/service.yml")); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}
} @Profile("development")
public static class DevelopmentConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertiesDev() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("/development/service.yml")); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}
} @Profile("production")
public static class ProductionConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertiesProd() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("/production/service.ml")); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
return propertySourcesPlaceholderConfigurer;
}
}
}
2. POJO object
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;@Component
@ConfigurationProperties(prefix = "enterprise-info")
@Data
public class ExternalEnterprise {
private String stringProp1;
private String stringProp2;
private Integer intProp1;
private List<String> listProp;
private Map<String, String> mapProp;
}
3. enternal-config.yml
enterprise-info:
stringProp1: propValue1
stringProp2: propValue2
intProp1: 10
listProp:
- listValue1
- listValue2
mapProp:
key1: mapValue1
key2: mapValue2Result
ExternalEnterprise(stringProp1=propValue1, stringProp2=propValue2, intProp1=10, listProp=[listValue1, listValue2], mapProp={key2=mapValue2, key1=mapValue1})위와 같이 오브젝트에 맵핑하여 프로퍼티 정보를 사용 할 수 있다.
