You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring多Profile配置文件优先级疑问:属性覆盖规则咨询

Spring Boot Multi-Profile Property Priority Explained

Great question! Let me break down exactly how Spring Boot resolves conflicting properties when you have multiple active profiles like dev and personal.

First, let's recap the base loading order:

  • Spring Boot always starts by loading the default application.properties (or .yml) file, regardless of active profiles.
  • Next, it loads the property files corresponding to each active profile, and these will override any matching properties from the default file.

Now, for the key part when multiple profiles are active:
When you specify profiles in a list (like spring.profiles.active=dev,personal), the last profile in the list has the highest priority. That means:

  • Any property defined in application-personal.properties will override the same property in application-dev.properties
  • Both dev and personal profile properties will override the same property in the default application.properties

Let me give you a concrete example to make it clear:

  1. Default application.properties has app.api.url=https://default-api.com
  2. application-dev.properties has app.api.url=https://dev-api.com
  3. application-personal.properties has app.api.url=https://personal-api.com

If you run with spring.profiles.active=dev,personal, the final value of app.api.url will be https://personal-api.com—because personal is the last profile in the active list, its properties take precedence over dev and the default.

A quick way to verify this is to inject the property into a bean and log it, or inspect the Environment object in your application:

@RestController
public class PropertyTestController {
    @Value("${app.api.url}")
    private String apiUrl;

    @GetMapping("/api-url")
    public String getApiUrl() {
        return apiUrl;
    }
}

Hitting this endpoint will show you the resolved value directly.

One more thing to note: this priority rule applies regardless of how you activate the profiles—whether it's via the spring.profiles.active property, command line arguments (--spring.profiles.active=dev,personal), or even programmatically in your application. The last profile added to the active set will always win out for conflicting properties.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:46:40