본문으로 바로가기

@ConfigurationProperties

category Backend/Spring 2017. 8. 21. 12:12
    반응형

    1. Value("")는 complex object를 사용할 수 없다.


    Spring Boot provides a very neat way to load properties for an application. Consider a set of properties described using YAML format:

    prefix:
        stringProp1: propValue1
        stringProp2: propValue2
        intProp1: 10
        listProp:
            - listValue1
            - listValue2
        mapProp:
            key1: mapValue1

    key2: mapValue2

    It has taken me a little while, but I do like the hierarchical look of the properties described in a YAML format.

    So now, given this property file, a traditional Spring application would have loaded up the properties in the following way:

    public class SamplePropertyLoadingTest {
        @Value("${prefix.stringProp1}")
        private String stringProp1;



    2. @ConfigurationProperties를 사용하여야 map, list 맵핑이 가능하다.


    1. setter가 있어야 주입이 가능하다.

    1. Springboot 2.0에서는 Scope가 생성되서 Setter로 주입이 되었다.

    2. Springboot 1.5.6에서는 Scope가 생성이 되지 않았다. 그래서 Scope를 넣어주고, Getter를 만들어줘야 했다. (3시간 삽질 )


    @Data public class Scope { private List<String> userOauthScope; private List<String> serviceOauthScope; }


    Spring 2.0

    : 기본 타입이 외에도 객체 생성 후 주입이 가능하다.


    @ConfigurationProperties(prefix = "google.oauth") public class GoogleAuthServiceImpl implements GoogleAuthService, InitializingBean { private static final JsonFactory JSON_FACTORY = new JacksonFactory(); private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); @Setter private Scope scope;


    Spring 1.5.6

    : 기본타입 외에는 객체 생성이 되지 않는다.


    @ConfigurationProperties(prefix = "google.oauth")
    public class GoogleAuthServiceImpl implements GoogleAuthService, InitializingBean {
    
    	private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    
    	private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    
    	@Getter
    	private Scope scope = new Scope();
    


    : 기본타입은 객체 생성 후 주입이 된다.

    @ConfigurationProperties(prefix = "google.oauth")
    public class GoogleAuthServiceImpl implements GoogleAuthService, InitializingBean {
    
    	private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    
    	private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    
    	@Getter
    	private Scope scope = new Scope();
    
    	@Setter
    	private String clientSecretPath;
    	
    	@Setter
    	private String serviceSecretPath;
    





    출처사이트


    https://dzone.com/articles/spring-boot-configurationproperties-1

    반응형