我使用SpringBoot应用程序,该应用程序必须在不同的环境下运行。属性文件是创建的,一旦我修改了环境,默认值就被适当的值覆盖。没关系。Spring Boot覆盖来自用户特定文件的属性
在下一步我想检查登录用户System.getProperty("user.name")
是否有一个自定义属性文件。如果是这样,这些属性必须用他的属性重写。所以步骤应该是(假设当前情景模式为开发):
- 负载的application.properties
- 负载和application-dev.properties覆盖性能
- 如果用户有一个自定义属性文件(user.properties),加载并覆盖属性
我读了很多topcis,发现了两种可能的解决方案,但都没有成功。
- 将注释
@PropertySource("user.properties")
添加到配置类,该类应加载用户特定的属性文件并覆盖值。出于测试目的,我将server.port=1234
添加到user.properties,但是这被忽略了。 - 创建一个自定义PropertyPlaceholderConfigurer。尽管此bean已成功创建,但服务器端口未更改。
`
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholder() {
PropertyPlaceholderConfigurer propertyPlaceholder = new PropertyPlaceholderConfigurer();
propertyPlaceholder.setLocations(
new ClassPathResource("application.properties"),
new ClassPathResource("application-dev.properties"),
new ClassPathResource("user.properties"));
propertyPlaceholder.setIgnoreResourceNotFound(true);
propertyPlaceholder.setIgnoreUnresolvablePlaceholders(true);
return propertyPlaceholder;
}
我不知道如何前进。所以任何想法都非常受欢迎。
更新:我已经justed推演示代码到GitHub。也许有助于找到解决方案:https://github.com/aszidien/springboot。
'System.getProperty(“user.name”)''不会查找当前登录的用户,这将查找'user.name'键当前激活的配置文件。因此请首先清除您的问题。 – TheCurious
至于我的理解你在找什么,说约翰目前登录的用户,所以你想'john.properties'覆盖'application-profile.property'中的键'if john.properties' exists?这是你的要求吗? – TheCurious
抱歉误会,你的推测是正确的。这是要求。 – aszidien