Spring Boot:如何让两个组件类分别加载不同配置文件
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:
@ConfigurationPropertiesrequires getters and setters for the fields you're binding (you can use Lombok's@Dataannotation to auto-generate these and reduce boilerplate).- If you're using Spring Boot 2.2+, add
@ConfigurationPropertiesScanto your main application class to automatically detect and register@ConfigurationPropertiesbeans, 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

