以下抜粋の通り、spring-bootはデフォルトでは以下ディレクトリのapplication.propertiesないしapplication.yamlを読み込む。歴史的事情や、リファレンス中にもある通りKubernetes等の事情で、デフォルトの変更が望ましいケースがある。
- From the classpath
- From the current directory
- The current directory
- The config/ subdirectory in the current directory
- Immediate child directories of the config/ subdirectory
環境
- IntelliJ IDEA 2024.1 (Community Edition)
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.5'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}
指定方法
いつものデフォルトsrc/main/resources/application.propertiesを変更するから、そこに変更先を定義しても意味が無い(一敗)。プロパティファイルの決定が起動処理の具体的にどのあたりかは知らないが相当に早い段階なのは確かだろう。そういうわけで、spring-bootアプリケーションの外側から指定する必要がある。
具体的には以下あたりで、他にも存在するとは思うが、現実的には以下のいずれか一つを使用するだろう。私はIntellij IDEAでの指定方法をしょっちゅう忘れるので、そのスクリーンショットを参考に載せている。
| name | スクショ | 備考 |
|---|---|---|
| コマンドライン引数 | ![]() |
|
| JVMオプション | ![]() |
表示されていない場合はModify options -> Add VM Options |
| 環境変数 | ![]() |
https://docs.spring.io/spring-boot/reference/features/external-config.html#features.external-config.typesafe-configuration-properties.relaxed-binding.environment-variables |
詳細
spring.config.location - デフォルト場所の変更
プロパティファイルのデフォルト場所を変更する。これで場所を変更するとsrc/main/resources/application.propertiesは読みこまなくなる。以下はコマンドライン引数での指定例。
--spring.config.location=file:./external/app001.properties,file:./external/app002.properties
カンマ区切りで複数指定可能で後が前を上書きする。springのfile:とかclasspath:とかを使用可能。
ファイルとディレクトリとで若干挙動が異なる。下記のように、/で終わるディレクトリを指定する場合、その後ろにspring.config.nameを付加したファイルを読みこむ。つまり下記例だとデフォルトではfile:./external/application.properties(かfile:./external/application.yaml)を読みこむ事になる。
--spring.config.location=file:./external/
spring.profiles.activeとも連携可能。以下例の場合app001-dev.propertiesが使用される。
--spring.profiles.active=dev --spring.config.location=file:./external/app001.properties
spring.config.additional-location - 場所の追加
プロパティファイルの場所を追加する。spring.config.locationが変更に対し、こちらは追加となる。以下例だとapplication.propertiesとapp001.propertiesの両方を読み込み、spring.config.additional-locationがapplication.propertiesで定義しているプロパティがあれば上書きする。詳細な読込順序は https://docs.spring.io/spring-boot/reference/features/external-config.html#features.external-config.files で要確認。リファレンスによる使用例としては、デフォルト値をapplication.propertiesで定義して実行時にspring.config.additional-locationで上書き、らしい。
--spring.config.additional-location=file:./external/app001.properties
こちらも、カンマ区切り・ファイルとディレクトリの挙動・spring.profiles.active連携、の挙動は同様。
spring.confing.name - デフォルトファイル名の変更
プロパティファイルのデフォルト名applicationを変更する。以下例だと src/main/resources/app.properties を読み込む。
--spring.config.name=app
spring.config.locationの項でも触れたが、他と組合せが可能で、以下例だと./external/app.properties を読み込む。
--spring.config.name=app --spring.config.additional-location=file:./external/
その他
optional:
これを付与するとspring.config.location等で指定するファイルが存在しなくてもエラーにならなくなる。以下例の場合、./external/unknown.propertiesが存在してもしなくてもエラーにならない。
--spring.config.location=optional:file:./external/unknown.properties
ファイルが存在しないと下記のようなエラーになりspring-bootが起動しない。
Description: Config data resource 'file [external\unknown.properties]' via location 'file:./external/unknown.properties' does not exist Action: Check that the value 'file:./external/unknown.properties' is correct, or prefix it with 'optional:'
なお、spring.config.on-not-found=ignoreを指定するとこのエラーを無視してspring-bootを起動させる事もできる。
/*/ ワイルドカード
spring.config.locationやspring.config.additional-locationのディレクトリ指定で最後を */ にするとサブディレクトリに展開される。たとえば、下記のようなプロパティファイル配置として、
- custom-config/redis/application.properties
- custom-config/mysql/application.properties
以下にすると上記2つのapplication.propertiesが読み込まれる。
--spring.config.location=file:./custom-config/*/


