5.3.4 @ConfigurationProperties를 사용하여 프로퍼티 직접 읽기

    LicenseService 클래스에서 getLicense() 메서드의 license.withComment() 값을 config.getProperty() 클래스 값으로 설정했다는 것을 알 수 있다. 해당 코드는 다음과 같다.

    return license.withComment(config.getProperty());

    com.optimagrowth.license.config.ServiceConfig.java 파일 클래스를 보면 다음과 같은 애너테이션이 있다는 것을 알 수 있다. 코드 5-11에서 여기에 사용할 @ConfigurationProperties 애너테이션을 보여 준다.

    @ConfigurationProperties(prefix="example")

     

    코드 5-11 ServiceConfig를 사용하여 애플리케이션 프로퍼티를 한곳으로 통합하기

    package com.optimagrowth.license.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    import lombok.Getter;
    import lombok.Setter;
    
    @Configuration
    @ConfigurationProperties(prefix="example")
    @Getter @Setter
    public class ServiceConfig {
    
        private String property;
        
    }
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.