HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🤩
개발
/
Spring
Spring
/
🎧
Spring Framework 핵심개념(Core)
/
⛑️
Environment(Properties & Profile)
/
🏉
@ConfigurationProperties
🏉

@ConfigurationProperties

사용방식yaml 특정 파일 지정 하는 방법(@PropertySource와 YamlPropertySourceFactory)
  • 스프링부트에서 외부 속성을 통해서 별도의 빈을 만들 수 있게 지원하는 어노테이션임
  • @Value방식 : 프로젝트가 작을 때에는 하나씩 할당 해 주어서 가능
  • @ConfigurationProperties : 전체적으로 매핑해서 그룹화해서 타입으로 사용하고 싶을 때 이용
  • @Value방식으로는 array에 대한 매핑이 안됨(support-vendors)
  • http://daplus.net/java-spring-boot-두-개의-데이터-소스-구성-및-사용/

사용방식

근데 이 방식은 setter로 뚫려 있어서 굳이 안쓰는게 나을 것 같기도 함
  1. @ConfigurationProperties 붙여줌(해당 어노테이션은 spring boot용 이기 때문에 @EnableConfigurationProperties 어노테이션도 추가로 붙여주어야 함)
  1. yaml 파일의 이름과 똑같이 필드이름 만들어주고(이때, prefix부분 붙여주어야 함)
  1. gettter와 setter만들어주면 알아서 property값들이 매핑이 됨
@Configuration @ConfigurationProperties(prefix = "kdt") @PropertySource(value="application.yaml", factory = YamlPropertySourceFactory.class) @EnableConfigurationProperties public class Properties implements InitializingBean { public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public int getMinimumOrderAmount() { return minimumOrderAmount; } public void setMinimumOrderAmount(int minimumOrderAmount) { this.minimumOrderAmount = minimumOrderAmount; } public List<String> getSupportVendors() { return supportVendors; } public void setSupportVendors(List<String> supportVendors) { this.supportVendors = supportVendors; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } private String version; private int minimumOrderAmount; private List<String> supportVendors; private String description; @Override public void afterPropertiesSet() throws Exception { System.out.println("version : " + version); System.out.println("minOrderAmount : " + minimumOrderAmount); System.out.println("list : " + supportVendors); System.out.println("description : " + description); } }
TroubleShooting
  • application.yml로 했을 때 ConfigurationProperties가 적용이 안되는 경우가 있었음(jwt 매핑시) → application.yaml로 변경하고 동작시키니 잘됨
    • 찾아본 클래스들
      • package org.springframework.boot.context.properties.bind; Binder 329 line
      • StandardConfigDataLocationResolver 생성자 부분
      • ConfigDataEnvironment

yaml 특정 파일 지정 하는 방법(@PropertySource와 YamlPropertySourceFactory)

@Configuration @ConfigurationProperties(prefix = "mail") @PropertySource(value = "classpath:mail.yaml", factory = YamlPropertySourceFactory.class) @Setter @Getter public class SettlementProperties { @Getter @Setter private Erp erp; public static class Erp { @Getter @Setter String url; } } 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); } private String getNameForResource(Resource resource) { String name = resource.getDescription(); if (!StringUtils.hasText(name)) { name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource); } return name; } }