본문 바로가기
카테고리 없음

[Spring] @ConfigurationProperties 사용하기

by 위시우 2024. 8. 13.

팀원이 kakao 관련 value 들을 주입할 때 사용해서 알게 되었다.

 

oauth:
  kakao:
    client:
      id: "idValue"
      secret: "secretValue"
      redirectUri: "redirectUriValue"
      grantType: "grantTypeValue"
      admin: "adminValue"

 

위의 application.yml 은 아래와 같이 변경될 수 있다. 

 

configuration properties 를 POJO를 통해 분리하는 것을 권장한다.

@ConfigurationProperties 는 동일한 prefix 를 갖는 hierarchical properties 를 다루는데 편하다. 

@ConfigurationProperties(prefix = "oauth.kakao.client")
@Getter
@Setter
public class KakaoProperties {
    private String id;
    private String secret;
    private String redirectUri;
    private String grantType;
    private String admin;
}

 

스프링 2.2 버젼부터 @ConfigurationProperties 가 달려있는 class 는 @EnableConfigurationProperties 또는 @Component 대신 @ConfigurationPropertiesScan 을 통해 class path 를 찾을 수 있다.
그러므로 @Component 나 @Configruation 과 같은 annotation 을 붙이지 않아도 되고 @EnabledConfigurationProperties 또 사용할 필요가 없다.

 

출처 : https://blog.yevgnenll.me/posts/spring-configuration-properties-fetch-application-properties

댓글