사용자 도구

사이트 도구


springframework:springboot:properties

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
다음 판 양쪽 다음 판
springframework:springboot:properties [2021/01/12 23:54]
kwon37xi [모든 properties 로그로 남기기]
springframework:springboot:properties [2021/01/21 22:00]
kwon37xi [YAML을 ''@PropertySource''로 사용하기]
줄 93: 줄 93:
   * [[https://github.com/spring-projects/spring-framework/blob/master/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.java|YamlPropertiesFactoryBean]] 를 사용하여 yaml을 ''Property'' 객체로 적재할 수 있다. ''PropertySource''로 등록되는 것은 아니므로 헷갈리면 안된다.   * [[https://github.com/spring-projects/spring-framework/blob/master/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.java|YamlPropertiesFactoryBean]] 를 사용하여 yaml을 ''Property'' 객체로 적재할 수 있다. ''PropertySource''로 등록되는 것은 아니므로 헷갈리면 안된다.
   * 이 경우 ''@Value("#propertyObjectName['key']")'' 형태로 사용가능해 진다.   * 이 경우 ''@Value("#propertyObjectName['key']")'' 형태로 사용가능해 진다.
 +
 +===== YAML을 ''@PropertySource''로 사용하기 =====
 +  * [[https://github.com/spring-projects/spring-framework/blob/master/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBean.java|YamlPropertiesFactoryBean]] 로 명시적으로 지정해줄 수 있다.
 +  * [[https://www.baeldung.com/spring-yaml-propertysource|@PropertySource with YAML Files in Spring Boot | Baeldung]]
 +
 +<code java>
 +@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>
 ===== @ConfigurationProperties ===== ===== @ConfigurationProperties =====
   * ''@EnableConfigurationProperties'' 가 설정돼 있어야 한다.   * ''@EnableConfigurationProperties'' 가 설정돼 있어야 한다.
springframework/springboot/properties.txt · 마지막으로 수정됨: 2024/03/04 16:27 저자 kwon37xi