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

Spring Boot:如何让两个组件类分别加载不同配置文件

Fixing Spring Boot SAML Config Loading: Separate Properties for Multiple Components

I see the core issue here - when you use @PropertySource on both components, Spring loads all those properties into the global Environment object. Since your placeholder keys (like placeholder1) are identical across both files, the later-loaded properties (from propertiesB.properties) overwrite the earlier ones from propertiesA.properties. That's why both configA and configB end up using the B properties.

Here's the proper way to isolate each component's configuration:


Step 1: Add Unique Prefixes to Your Properties Files

First, update your properties files to use distinct prefixes so their keys don't clash in the global Environment:

propertiesA.properties:

a.placeholder1 = URL for destination
a.placeholder2 = Path to keystore

propertiesB.properties:

b.placeholder1 = URL for other destination
b.placeholder2 = Path to other keystore

Step 2: Use @ConfigurationProperties with @PropertySource

Instead of relying on @Value, use @ConfigurationProperties to bind properties directly to your component fields, paired with @PropertySource to specify the exact file for each class. This keeps each component's configuration fully isolated.

Updated Class A:

@Component("classAProperties")
@PropertySource("classpath:propertiesA.properties")
@ConfigurationProperties(prefix = "a")
public class A implements ConfigInterface {
    private String placeholder1;
    private String placeholder2;

    // Getters and setters (required for @ConfigurationProperties to work)
    public String getPlaceholder1() {
        return placeholder1;
    }

    public void setPlaceholder1(String placeholder1) {
        this.placeholder1 = placeholder1;
    }

    public String getPlaceholder2() {
        return placeholder2;
    }

    public void setPlaceholder2(String placeholder2) {
        this.placeholder2 = placeholder2;
    }
}

Updated Class B:

@Component("classBProperties")
@PropertySource("classpath:propertiesB.properties")
@ConfigurationProperties(prefix = "b")
public class B implements ConfigInterface {
    private String placeholder1;
    private String placeholder2;

    // Getters and setters (required for @ConfigurationProperties to work)
    public String getPlaceholder1() {
        return placeholder1;
    }

    public void setPlaceholder1(String placeholder1) {
        this.placeholder1 = placeholder1;
    }

    public String getPlaceholder2() {
        return placeholder2;
    }

    public void setPlaceholder2(String placeholder2) {
        this.placeholder2 = placeholder2;
    }
}

Step 3: Keep Your Controller Injection As-Is

Your existing controller code works without any changes—Spring will inject the correct bean with its isolated configuration:

@Controller
public class controller {
    @Autowired
    @Qualifier("classAProperties")
    private ConfigInterface configA;
    @Autowired
    @Qualifier("classBProperties")
    private ConfigInterface configB;

    public methodA ( ... ) {
        ... createAMessage(configA); ...
    }

    public methodB ( ... ) {
        ... createAMessage(configB); ...
    }
}

Key Notes:

  • @ConfigurationProperties requires getters and setters for the fields you're binding (you can use Lombok's @Data annotation to auto-generate these and reduce boilerplate).
  • If you're using Spring Boot 2.2+, add @ConfigurationPropertiesScan to your main application class to automatically detect and register @ConfigurationProperties beans, simplifying setup further.
  • This approach ensures each component only uses its intended properties file, with no overlap or overwriting in the global Environment.

内容的提问来源于stack exchange,提问作者André Schmidt

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:49:46