[スニペット] 1つのapplication.ymlで複数の環境に合わせて設定ファイルを読み込む：SpringBoot


```
src/
└── main/
    ├── java/com/example/demo/
    │   ├── DemoApplication.java
    │   ├── config/
    │   │   └── AppConfig.java       ← バインディングクラス
    │   └── controller/
    │       └── SampleController.java ← 動作確認用コントローラ
    └── resources/
        └── application.yml          ← 設定ファイル（1ファイルのみ）
```
```yaml
spring:
  profiles:
    active: sit

custom:
  api-url:
    ut: https://api-ut.example.com
    it: https://api-it.example.com
    sit: https://api-sit.example.com
    vst: https://api-vst.example.com
    uat: https://api-uat.example.com
    prod: https://api.example.com

  datasource:
    url:
      ut: jdbc:mysql://ut-db:3306/db
      it: jdbc:mysql://it-db:3306/db
      sit: jdbc:mysql://sit-db:3306/db
      vst: jdbc:mysql://vst-db:3306/db
      uat: jdbc:mysql://uat-db:3306/db
      prod: jdbc:mysql://prod-db:3306/db
```
```java
@Component
@ConfigurationProperties(prefix = "custom")
public class AppConfig {

    private Map<String, DataSourceConfig> datasource;
    private Map<String, String> apiUrl;

    public Map<String, DataSourceConfig> getDatasource() {
        return datasource;
    }

    public void setDatasource(Map<String, DataSourceConfig> datasource) {
        this.datasource = datasource;
    }

    public Map<String, String> getApiUrl() {
        return apiUrl;
    }

    public void setApiUrl(Map<String, String> apiUrl) {
        this.apiUrl = apiUrl;
    }

    public DataSourceConfig getDatasourceForProfile(String profile) {
        return datasource.get(profile);
    }

    public String getApiUrlForProfile(String profile) {
        return apiUrl.get(profile);
    }
}

```
```java
package com.example.demo.controller;

import com.example.demo.config.AppConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/config")
public class SampleController {

    private final AppConfig appConfig;

    @Value("${spring.profiles.active}")
    private String activeProfile;

    public SampleController(AppConfig appConfig) {
        this.appConfig = appConfig;
    }

    @GetMapping
    public String getConfig() {
        String apiUrl = appConfig.getApiUrlForProfile(activeProfile);
        String datasourceUrl = appConfig.getDatasourceUrlForProfile(activeProfile);
        return String.format("Active Profile: %s\nAPI URL: %s\nDB URL: %s", activeProfile, apiUrl, datasourceUrl);
    }
}
```
```java
public class DataSourceConfig {
    private String url;
    private String username;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
```
```java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@SpringBootApplication
@ConfigurationPropertiesScan  // ← AppConfig の自動スキャン
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
```