我想添加一个新的属性源,可用于读取应用程序中的属性值。我想使用 Spring 来做到这一点。我在@Configuration 类中有一段这样的代码:
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
MutablePropertySources sources = new MutablePropertySources();
MyCustomPropertySource propertySource = new MyCustomPropertySource("my custom property source");
sources.addFirst(propertySource);
properties.setPropertySources(sources);
return properties;
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作得很好。但是,它也在做的是覆盖我不想覆盖的其他属性值(例如,spring boot 使用的 application.properties 文件中的 server.port 属性)。因此,基本问题是添加此属性源但不使其覆盖其他属性的最佳方法是什么。有什么方法可以获取现有的属性源并简单地添加它?