두 손끝의 창조자

test active profile로 @PropertySources 설정 본문

Spring

test active profile로 @PropertySources 설정

codinglog 2022. 5. 27. 14:07

프로파일 별로 다른 DB접속 정보를 사용하기 위해서 아래와 같이 설정하였다.

스프링 부트에 property 파일을 이용해서 설정하던디 -D를 이용해서 환경설정으로 실행했을 때는 문제없이 수행된다.

@Configuration
@PropertySources({
        @PropertySource("classpath:/properties/${spring.profiles.active:local}.properties"),
        @PropertySource("classpath:/properties/db/biz/${spring.profiles.active:local}.properties"),
        @PropertySource("classpath:/properties/db/frm/${spring.profiles.active:local}.properties"),
})
public class ApplicationConfig {

}

하지만 테스트에서 프로퍼티를 설정해서 하고자 할 때

@ActiveProfiles({"tst"})

를 지정하면 프로파일 자체는 설정이 되지만 @PropertySources 에 매핑이 제대로 되지 않는다. 설정하는 타이밍이 달라서 그런듯 한데 처음부터 java configuration 을 설정할 때 프로파일 별로 등록하게 하면 해결된다.

@Configuration
public class ApplicationConfig {
    @Configuration
    @Profile("local")
    @PropertySources({
            @PropertySource("classpath:/properties/local.properties"),
            @PropertySource("classpath:/properties/db/biz/local.properties"),
            @PropertySource("classpath:/properties/db/frm/local.properties"),
    })
    static class LocalApplicationConfig {
    }

    @Configuration
    @Profile("tst")
    @PropertySources({
            @PropertySource("classpath:/properties/tst.properties"),
            @PropertySource("classpath:/properties/db/biz/tst.properties"),
            @PropertySource("classpath:/properties/db/frm/tst.properties"),
    })
    static class TestApplicationConfig {
    }

    @Configuration
    @Profile("prd")
    @PropertySources({
            @PropertySource("classpath:/properties/prd.properties"),
            @PropertySource("classpath:/properties/db/biz/prd.properties"),
            @PropertySource("classpath:/properties/db/frm/prd.properties"),
    })
    static class PrdApplicationConfig {
    }
}
반응형
Comments