Spring Boot Multiple yml with profiles

I just wanted to use multiple config(app-key.yml, app-api.yml) for each environment.
複数の設定ファイル(例:app-key.yml, app-api.yml)を各環境ごとで使用できるようにしています。

f:id:tomoTaka:20171217074605p:plain

each file has the below value
各ファイルの値は以下のようにしています

app-key.yml

environment default local prod
key val-default
key1 val1-default val1-local
key2 val2-default val2-prod

app-api.yml

environment default local prod
timeout 3
retry-count 1 5 3

local環境を設定して起動した時の値を表示
The below shows the value for local environment.

--spring.profiles.active=local

f:id:tomoTaka:20171216220528p:plain

prod環境を設定して起動した時の値を表示
The below shows the value for prod environment.

--spring.profiles.active=prod

f:id:tomoTaka:20171216220747p:plain

default設定を基本的には使用し、環境ごとの設定を上書きするために以下のように@PropertySourceで、デフォルトファイル、上書きするために環境(profile)に依存したファイルを設定
You can use PropertySource annotation which has two files, to override default file using the second one.

AppKeyConfig.java

@Configuration
@PropertySource({"classpath:/config/app-key.yml","classpath:/config/app-key-${spring.profiles.active}.yml"})
@ConfigurationProperties
public class AppKeyConfig {
    private String key;
    private String key1;
    private String key2;
...

AppApiConfig.java

@Configuration
@PropertySource({"classpath:/config/app-api.yml","classpath:/config/app-api-${spring.profiles.active}.yml"})
@ConfigurationProperties
public class AppApiConfig {
    private int timeout;
    private int retryCount;
...

テスト時には、@SpringBootTestアノテーションで環境を指定できます。
When testing, @SpringBootTest(properties = "spring.profiles.active=local") works fine.

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=local")
public class AppKeyConfigLocalTest {
    @Autowired
    private AppKeyConfig appKeyConfig;

    @Test
    public void keyShouldBeDefaultValue(){
        String key = appKeyConfig.getKey();
        assertThat(key).isEqualTo("val-default");
    }
    @Test
    public void key1ShouleBeLocalValue(){
        String key1 = appKeyConfig.getKey1();
        assertThat(key1).isEqualTo("val1-local");
    }
...

code is here
github.com