사용자 도구

사이트 도구


springframework:springboot:properties

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
springframework:springboot:properties [2021/01/15 15:01]
kwon37xi [YAML을 Property 객체로 변환하기]
springframework:springboot:properties [2024/03/04 16:27] (현재)
kwon37xi
줄 1: 줄 1:
 ====== SpringBoot Properties ====== ====== SpringBoot Properties ======
   * [[springframework:springboot|SpringBoot]] 에서 설정 값 외재화   * [[springframework:springboot|SpringBoot]] 에서 설정 값 외재화
 +  * [[springframework:springboot:properties_migrator|spring-boot-properties-migrator]]
   * 보통 ''yml''을 이용해서 ''application.yml''로 지정한다.   * 보통 ''yml''을 이용해서 ''application.yml''로 지정한다.
 +  * ''YamlPropertiesFactoryBean''
 +  * ''PropertiesFactoryBean''
 +  * ''YamlPropertySourceLoader''
 +  * ''PropertiesPropertySourceLoader''
 +
 +===== 기본값 설정 =====
 +  * 이미 지정된 property 이면, 해당 값을 사용하고, 그게 아니면 기본값을 사용하는 구조로
 +  * ''EXTERNAL_USERNAME/EXTERNAL_PASSWORD'' 환경 변수 혹은 ''external.username/external.password'' 프라퍼티가 존재하면 그 값을 사용하고 그게 아니면 '':'' 뒤에 지정된 값을 사용한다.
 +<code yml>
 +testing:
 +  my:
 +    username: ${external.username:system}
 +    password: ${external.password:test}
 +</code>
 +  * [[https://stackoverflow.com/a/49644607/1051402|SpEL로 기본값 지정]]. SpEL은 ''@Value''로 값을 읽는 경우에만 지정 가능하며, ''@ConfigurationProperties''를 사용할 때는 안 된다. 혼란스럽기 때문에 안 사용하는게 나을듯 하다.
 +
 +<code yml>
 +testing:
 +  my:
 +    username: ${external.username:user}
 +    password: ${external.password:#{' test'}}
 +</code>
 +<code java>
 +@Value("${testing.my.username}")
 +private String username;
 +
 +@Value("${testing.my.password}")
 +private String password;
 +</code>
  
 ===== profile 별 설정 ===== ===== profile 별 설정 =====
줄 98: 줄 128:
   * [[https://www.baeldung.com/spring-yaml-propertysource|@PropertySource with YAML Files in Spring Boot | Baeldung]]   * [[https://www.baeldung.com/spring-yaml-propertysource|@PropertySource with YAML Files in Spring Boot | Baeldung]]
  
-<code sh+<code java
-@PropertySource(value = "classpath:foo.yml", factory = YamlPropertySourceFactory.class)+@PropertySource( 
 +    value = "classpath:foo.yml",  
 +    factory = YamlPropertySourceFactory.class 
 +) 
 +</code> 
 + 
 +<code java> 
 +public class YamlPropertySourceFactory implements PropertySourceFactory { 
 + 
 +    @Override 
 +    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource)  
 +      throws IOException { 
 +        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); 
 +        factory.setResources(encodedResource.getResource()); 
 + 
 +        Properties properties = factory.getObject(); 
 + 
 +        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties); 
 +    } 
 +
 </code> </code>
 ===== @ConfigurationProperties ===== ===== @ConfigurationProperties =====
줄 181: 줄 231:
   com.baeldung.environmentpostprocessor.PriceCalculationEnvironmentPostProcessor   com.baeldung.environmentpostprocessor.PriceCalculationEnvironmentPostProcessor
 </code> </code>
 +
 +===== YAML Duration =====
 +  * Spring Boot 2.1 부터 [[https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html|Duration]] 을 지정할 수 있다.
 +  * ''100ms'' 형태로 시간 단위를 문자로 지정하면 된다.
 +  * ''ns'' for nanoseconds
 +  * ''us'' for microseconds
 +  * ''ms'' for milliseconds
 +  * ''s'' for seconds
 +  * ''m'' for minutes
 +  * ''h'' for hours
 +  * ''d'' for days
  
 ===== 참고 ===== ===== 참고 =====
springframework/springboot/properties.1610690485.txt.gz · 마지막으로 수정됨: 2021/01/15 15:01 저자 kwon37xi