문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
springframework:springboot:properties [2019/03/18 15:35] kwon37xi [@ConfigurationProperties] |
springframework:springboot:properties [2024/06/12 09:00] (현재) kwon37xi |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== SpringBoot Properties ====== | ====== SpringBoot Properties ====== | ||
* [[springframework: | * [[springframework: | ||
+ | * [[java: | ||
+ | * [[springframework: | ||
* 보통 '' | * 보통 '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== 기본값 설정 ===== | ||
+ | * 이미 지정된 property 이면, 해당 값을 사용하고, | ||
+ | * '' | ||
+ | <code yml> | ||
+ | testing: | ||
+ | my: | ||
+ | username: ${external.username: | ||
+ | password: ${external.password: | ||
+ | </ | ||
+ | * [[https:// | ||
+ | |||
+ | <code yml> | ||
+ | testing: | ||
+ | my: | ||
+ | username: ${external.username: | ||
+ | password: ${external.password:# | ||
+ | </ | ||
+ | <code java> | ||
+ | @Value(" | ||
+ | private String username; | ||
+ | |||
+ | @Value(" | ||
+ | private String password; | ||
+ | </ | ||
===== profile 별 설정 ===== | ===== profile 별 설정 ===== | ||
줄 32: | 줄 63: | ||
===== profile include ===== | ===== profile include ===== | ||
* 프로필을 자동 활성화 한다. | * 프로필을 자동 활성화 한다. | ||
+ | * Spring Boot 2.3 이전에서 사용하는 방식. | ||
+ | * 2.4 부터는 [[https:// | ||
* '' | * '' | ||
spring.profiles: | spring.profiles: | ||
줄 91: | 줄 124: | ||
* [[https:// | * [[https:// | ||
* 이 경우 '' | * 이 경우 '' | ||
+ | |||
+ | ===== YAML을 '' | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | <code java> | ||
+ | @PropertySource( | ||
+ | value = " | ||
+ | factory = YamlPropertySourceFactory.class | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | public class YamlPropertySourceFactory implements PropertySourceFactory { | ||
+ | |||
+ | @Override | ||
+ | public PropertySource<?> | ||
+ | throws IOException { | ||
+ | YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); | ||
+ | factory.setResources(encodedResource.getResource()); | ||
+ | |||
+ | Properties properties = factory.getObject(); | ||
+ | |||
+ | return new PropertiesPropertySource(encodedResource.getResource().getFilename(), | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
===== @ConfigurationProperties ===== | ===== @ConfigurationProperties ===== | ||
* '' | * '' | ||
줄 100: | 줄 161: | ||
* '' | * '' | ||
* '' | * '' | ||
+ | ===== PropertySource과 properties 로그로 남기기 ===== | ||
+ | * [[https:// | ||
+ | |||
+ | <code java> | ||
+ | @Component | ||
+ | public class LoadedConfigFileListener implements ApplicationListener< | ||
+ | |||
+ | @Override | ||
+ | public void onApplicationEvent(ApplicationReadyEvent event) { | ||
+ | MutablePropertySources propertySources = event.getApplicationContext().getEnvironment().getPropertySources(); | ||
+ | Iterator< | ||
+ | propertySourceIterator.forEachRemaining(propertySource -> log.info(" | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public int getOrder() { | ||
+ | return ConfigFileApplicationListener.DEFAULT_ORDER + 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
===== 모든 properties 로그로 남기기 ===== | ===== 모든 properties 로그로 남기기 ===== | ||
* [[https:// | * [[https:// | ||
- | * 아래와 같이 '' | + | * 최종적으로 결정된 properties 목록을 확인하고자 할 때, 아래와 같이 '' |
<code java> | <code java> | ||
@Component | @Component | ||
줄 137: | 줄 220: | ||
* 시스템 프라퍼티로 : '' | * 시스템 프라퍼티로 : '' | ||
* 환경 변수로 : '' | * 환경 변수로 : '' | ||
+ | |||
+ | |||
+ | ===== EnvironmentPostProcessor 를 통한 프라퍼티 값 후처리 ===== | ||
+ | * [[https:// | ||
+ | * 예를들면, | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * 작성한 코드를 '' | ||
+ | org.springframework.boot.env.EnvironmentPostProcessor= | ||
+ | com.baeldung.environmentpostprocessor.PriceCalculationEnvironmentPostProcessor | ||
+ | </ | ||
+ | |||
+ | ===== YAML Duration ===== | ||
+ | * Spring Boot 2.1 부터 [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
===== 참고 ===== | ===== 참고 ===== |