0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2025-04-04
src/
└── main/
    ├── java/com/example/demo/
    │   ├── DemoApplication.java
    │   ├── config/
    │   │   └── AppConfig.java       ← バインディングクラス
    │   └── controller/
    │       └── SampleController.java ← 動作確認用コントローラ
    └── resources/
        └── application.yml          ← 設定ファイル(1ファイルのみ)
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
@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);
    }
}

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);
    }
}
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;
    }
}
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);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?