Java 使用Spring在不同的属性文件中使用相同的键

Java 使用Spring在不同的属性文件中使用相同的键,java,spring,spring-boot,properties-file,Java,Spring,Spring Boot,Properties File,假设我有两个属性文件: prop1.属性 name=foo name=bar prop2.属性 name=foo name=bar 我想使用将这些属性注入到两个不同的类中。我目前的做法是: Foo.java @Component @PropertySource("prop1.properties") @ConfigurationProperties public class Foo { private String name; // Getter & sett

假设我有两个属性文件:

prop1.属性

name=foo
name=bar
prop2.属性

name=foo
name=bar
我想使用将这些属性注入到两个不同的类中。我目前的做法是:

Foo.java

@Component
@PropertySource("prop1.properties")
@ConfigurationProperties
public class Foo {

    private String name;

    // Getter & setter
}
@Component
@PropertySource("prop2.properties")
@ConfigurationProperties
public class Bar {

    private String name;

    // Getter & setter
}
Bar.java

@Component
@PropertySource("prop1.properties")
@ConfigurationProperties
public class Foo {

    private String name;

    // Getter & setter
}
@Component
@PropertySource("prop2.properties")
@ConfigurationProperties
public class Bar {

    private String name;

    // Getter & setter
}

当我测试它时,我在两个类中都得到了name
bar
,这意味着两个类都在查找每个
@PropertySource
prop2上定义的所有属性文件。属性优先。我知道在键上添加前缀是一种解决方案,但我想避免这种情况。

您解决了这个问题吗?我也面临同样的问题