You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot读取application-dev.properties与application.properties的规则是什么?

Hey there! Let's clear up this confusion for you—this behavior is actually a combination of Spring Boot's core configuration rules and how you've set up your IntelliJ IDEA run configurations. Let's break it down step by step:

Spring Boot's Core Profile Activation Rules (The Foundation)

Spring Boot uses a priority-based system to load configuration files, and the key here is which "profile" is active:

  • By default, Spring Boot will always load application.properties as the base configuration.
  • If you activate a specific profile (like dev or uat), it will load the corresponding application-{profile}.properties file in addition to the base one—with the profile-specific file overriding any duplicate settings from the base.

There are several ways to activate a profile, and these are what's driving the inconsistent behavior you're seeing:

  • Static config file setting: Add spring.profiles.active=dev to your application.properties—this will hardcode the active profile unless overridden by a higher-priority setting.
  • Command line arguments: Pass --spring.profiles.active=dev when starting the app.
  • JVM system properties: Use -Dspring.profiles.active=dev as a VM argument.
  • Environment variables: Set the SPRING_PROFILES_ACTIVE system environment variable to your desired profile.
How IntelliJ IDEA Fits In

IDEA doesn't override Spring's rules—it just gives you a convenient way to set those higher-priority startup parameters without editing config files or typing command lines every time:

  • Open your Run/Debug Configurations (click the dropdown next to the run button, then "Edit Configurations").
  • For your Spring Boot app, check these areas:
    • VM options: If you've added -Dspring.profiles.active=dev here, this will override any spring.profiles.active setting in your application.properties.
    • Program arguments: If you've added --spring.profiles.active=uat here, this also takes priority over the config file.
    • Active Profiles (some IDEA versions have this in the main Configuration tab): Selecting a profile here is equivalent to setting the JVM property automatically.
Why You're Seeing Inconsistent Loads

The most likely scenarios are:

  • You’ve toggled the active profile in IDEA's run configs at some point (maybe for testing different environments) and forgot to set it back.
  • Your application.properties has a spring.profiles.active setting that's sometimes being overridden by IDEA's startup parameters.
  • You might have environment variables or global JVM settings that are interfering (though this is less common for local development).
Quick Troubleshooting Steps

To get consistent behavior every time:

  1. Check your application.properties for any spring.profiles.active line—remove it if you want IDEA to control the active profile, or set it to your default.
  2. Verify your IDEA run configuration: Make sure the VM options, program arguments, and active profiles are set to the profile you want.
  3. If you want to confirm which profile is active at startup, add a log statement in your app (like @Value("${spring.profiles.active}") private String activeProfile;) and print it on startup—this will tell you exactly which profile is being used.

内容的提问来源于stack exchange,提问作者Mikayil Abdullayev

火山引擎 最新活动