本番環境は環境変数、開発環境は設定ファイルで設定値を管理する
はじめに
パスワード設定の管理を下記のようにしたい - 開発用パスワードは設定ファイルで管理し、gitなどで開発者全員で共有したい - 本番用パスワードは本番環境の環境変数として管理し、共有しない
やった方法
- .envというファイルを作る
- .envファイルに本番環境の環境変数と同様のキー・値を登録する
- application-development.ymlを作る
- 作ったapplication-development.ymlに下記のプロパティを追加する
.envファイル内容 起動時にspring.profiles.active=developmentのプロファイルを設定して実行
MESSAGE=nihao4
application-development.yml
spring: config: import: optional:file:.env[.properties]
package com.example.properties import org.springframework.beans.factory.annotation.Value import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @RestController("/") class HelloController( @Value("\${MESSAGE}") val message: String ) { @GetMapping fun hello(): String { return message } }
結果
ローカル開発環境
curl http://localhost:8080
本番環境
MESSAGE=honban2で環境変数を登録してある