Web系開発メモ

Java, C#, HTML, CSS, JavaScript のことなどを書いてます。

SpringBoot 設定ファイルを環境ごとに切り替える方法

Spring Boot の application.properties を、環境に応じて切り替える方法を書いていきます。

バージョン

動作確認で使用した製品のバージョンは以下の通りです。

1. 設定ファイルの作成

今回は以下の設定ファイルを作成します。

  1. application.properties
  2. application-staging.properties

1が基本の設定ファイルで、2がステージング環境用です。2は差分だけ定義します。

1.1. application.properties

全ての環境で有効になる設定を定義します。

src/main/resources/application.properties

server.port=8080

spring.jackson.date-format=yyyy/MM/dd HH:mm:ss
spring.jackson.time-zone=Asia/Tokyo

1.2. application-staging.properties

ステージング環境で有効にしたい設定を定義します。

src/main/resources/application-staging.properties

server.port=80

ポート番号だけ上書きされて、Jacksonの設定はステージング環境でも有効になります。

2. アプリの起動

2.1. ステージング環境ではない場合

プロパティを指定しないで起動するか、IDEでアプリを起動します。

java -jar target/spring-switch-config-1.0.0.jar

2.2. ステージング環境の場合

プロパティ spring.profiles.active=staging を指定して起動します。

java -jar -Dspring.profiles.active=staging target/spring-switch-config-1.0.0.jar

3. 動作確認用の資源

動作確認で使用した資源は以下の通りです。

3.1. アプリ起動クラス

アプリを起動するクラスです。

src/main/java/org/example/SpringApp.java

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringApp {
  public static void main(String[] args) {
    SpringApplication.run(SpringApp.class, args);
  }
}

3.2. コントローラー

Jacksonの設定を確認するためのクラスです。

src/main/java/org/example/controller/DateController.java

package org.example.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DateController {
  @GetMapping("/date")
  public Map<String, Date> get() {
    return Map.of("date", new Date());
  }
}

3.3. ビルドファイル

Mavenのビルドファイルは以下の通りです。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.2</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>org.example</groupId>
  <artifactId>spring-switch-config</artifactId>
  <version>1.0.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>17</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>