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 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.propertiesas the base configuration. - If you activate a specific profile (like
devoruat), it will load the correspondingapplication-{profile}.propertiesfile 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=devto yourapplication.properties—this will hardcode the active profile unless overridden by a higher-priority setting. - Command line arguments: Pass
--spring.profiles.active=devwhen starting the app. - JVM system properties: Use
-Dspring.profiles.active=devas a VM argument. - Environment variables: Set the
SPRING_PROFILES_ACTIVEsystem environment variable to your desired profile.
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=devhere, this will override anyspring.profiles.activesetting in yourapplication.properties. - Program arguments: If you've added
--spring.profiles.active=uathere, 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.
- VM options: If you've added
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.propertieshas aspring.profiles.activesetting 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).
To get consistent behavior every time:
- Check your
application.propertiesfor anyspring.profiles.activeline—remove it if you want IDEA to control the active profile, or set it to your default. - Verify your IDEA run configuration: Make sure the VM options, program arguments, and active profiles are set to the profile you want.
- 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




