LoginSignup
77
80

More than 5 years have passed since last update.

Spring Bootで設定のプロファイル分けをする

Posted at

Spring Bootは、設定をapplication.propertiesやapplication.ymlで書ける。
Webの情報だと、propertiesでの設定例が多くて、YAMLの設定例が少ない印象。
基本一緒なんだけど、YAMLでの設定例を書いておく。

# spring.profiles.activeにデフォルトでアクティブなプロファイルを指定
spring:
  profiles:
    active: dev

# "---"でプロファイルを区切る
# プロファイル名はspring.profilesに書く
---
spring:
  profiles: dev
  datasource:
    url: jdbc:mysql://localhost/devdb
    username: db_user
    password: db_password
    driverClassName: com.mysql.jdbc.Driver
---
spring:
  profiles: production
  datasource:
    url: jdbc:mysql://production.com:3306/productdb
    username: db_user
    password: db_password
    driverClassName: com.mysql.jdbc.Driver

こうすると、devプロファイルで動作するようにjarが作られる。
実行時にproduction環境で動作させるには、以下のコマンドで実行する。

java -jar -Dspring.profiles.active=production hoge.jar
77
80
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
77
80