Spring에서 제공하는 기능과 Spring Boot에서 제공되는 기능 구분 주의!!
kdt.version=v1.0.0
kdt.support-vendors=a, b, c, d
kdt.minimum-order-amount=1
@PropertySource
PropertySource를 Spring에 추가하기(Spring의 Environment에 프로퍼티 파일의 속성을 주입)위해서 @Configuration와 함께 사용된다.
@Configuration
@PropertySource("application.properties")
설정된 protperties 사용
var applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
var environment = applicationContext.getEnvironment();
var version = environment.getProperty("kdt.version");
// Integer로 읽어오겠다.
var minimumOrderAmount = environment.getProperty("kdt.minimum-order-amount", Integer.class); 
// List로 읽어 오겠다.
var supportVendors = environment.getProperty("kdt.support-vendors", List.class); 
by @Value
@Component
public class OrderProperties {
  @Value("${kdt.version}")
  private String version;
  ...
}
property의 값을 객체에 필드에 주입하여 사용하는 방법
@Value()는 환경 변수 값을 읽어올 수도 있다.
환경 변수에 해당하는 값이 있는지 먼저 확인하고 properties 파일을 확인한다.
@Value()는 Bean에서만 사용할 수 있는가?
YES! 또한 생성자와 함수의 인자에도 적용할 수 있습니다. 
This annotation can be used for injecting values into fields in Spring-managed beans, and it can be applied at the field or constructor/method parameter level.
출처
@Value("${key}")가 타입이 맞지 않는 경우
exception 발생
@Value("${key}")가 존재하지 않는 key일 경우
key가 문자열로 들어간다
@Value("${key:default_value}")
key에 해당하는 값이 없는 경우 default_value 넣어라
YAML의 핵심은 데이터 중심이다. property를 작성하기 위해 사용한다.
작성 방식
kdt:
  version: "v1.0"
  minimum-order-amount: 1
  # 배열 작성 방식
  support-vendors:
    - a
    - b
    - c
    - d
  # 멀티 라인 작성 방법:|을 이용해 작성
  description: |
    line 1 hello world
    line 2 xxxxx 
사용 방법
PropertySourceFactory
@Configuration
@PropertySource(value = "application.yml", factory = YamlPropertiesFactory.class)
...
public class YamlPropertiesFactory implements PropertySourceFactory {
  @Override
  public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
      var yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
      yamlPropertiesFactoryBean.setResources(resource.getResource());
      var properties = yamlPropertiesFactoryBean.getObject();
      return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
  }
}
...
@Configuration
@ConfigurationProperties(prefix = "kdt") // kdt 하위 내용 필드로 주입
public class OrderProperties {
	private String version;
    ...
}
설정의 일부를 분리하여 특정 환경에서만 사용 가능하도록 한다.
@Profile + @Component → profile과 함께 Bean 등록
@Repository
@Profile("dev")
public class JdbcVoucherRepository implements VoucherRepository {...}
@Repository
@Profile({"local", "default"})
public class MemoryVoucherRepository implements VoucherRepository { ... }
var applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(AppConfiguration.class);
var environment = applicationContext.getEnvironment();
environment.setActiveProfiles("local"); // local 프로파일 사용
applicationContext.refresh();
# 공통 설정: 모든 profile에서 사용
servers:
  - dev.bar.com
  - foo.bar.com
---
# local profile인 경우 사용
spring.config.activate.on-profile: local
kdt:
  version: "v1.0"
  minimum-order-amount: 1
  support-vendors:
    - a
    - b
    - c
    - d
  description: |
    line 1 hello world
    line 2 xxxxx
---
# dev profile인 경우 사용
spring.config.activate.on-profile: dev
kdt:
  version: "v1.0"
  minimum-order-amount: 1
  support-vendors:
    - deva
    - devb
    - devc
    - devd
  description: |
    dev line 1 hello world
    dev line 2 xxxxx
 var springApplication = new SpringApplication(KdtApplication.class);
  springApplication.setAdditionalProfiles("local");--spring.profiles.active=devapplication-local.yml, application-dev.yml, application.yml모든 ApplicationContext가 ResourceLoader를 구현하고 있다 → ApplicationContext에서 Resource 객체를 가져올 수 잇다.
Resource와 Resource Loader를 통해 하나의 API로 이미지 파일, 텍스트 파일 등의 외부 리소스를 읽을 수 있도록 하였다.
var resource = applicationContext.getResource("classpath:application.yml");
var resource2 = applicationContext.getResource("file:test/sample.txt");
var resource3 = applicationContext.getResource("https://stackoverflow.com");
classpath
file
working directory 경로