같은 키의 외부 설정을 묶어서 하나의 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;
}
}
<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