같은 키의 외부 설정을 묶어서 하나의 bean으로 만드는 방법

@Component
@ConfigurationProperties("cmlee")
public class CmleeProperties {

	private String name;
	private int age;
	private String fullName;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
}
  1. @ConfigurationProperties() 추가
  2. 빈으로 등록
  3. 프로젝트를 빌드할 때 메타 정보를 빌드 해주는 플러그인 추가
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

사용

@Component
public class SampleListener implements ApplicationRunner {

	@Autowired
	private CmleeProperties cmleeProperties;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("=============");
		System.out.println(cmleeProperties.getName());
		System.out.println(cmleeProperties.getAge());
		System.out.println("=============");
	}
}

융통성 있는 바인딩

프로퍼티 타입 컨버전

기본적으로 자동으로 정의한 타입으로 컨버전 하여 입력된다.

특별한 시간정보를 위한 타입이 있다.

// CmleeProperties.class

@DurationUnit(ChronoUnit.SECONDS)
private Duration sessionTimeout = Duration.ofSeconds(30);
# application.properties
cmlee.sessionTimeout = 25s

프로퍼티 값 검증