gpt4 book ai didi

Spring Boot 应用程序特定的外部属性

转载 作者:行者123 更新时间:2023-11-28 22:04:39 25 4
gpt4 key购买 nike

我正在寻找这个问题的解决方案。

我有几个属性文件(application.properties、application-realdb.properties)。假设我从“realdb” Spring 配置文件开始。现在我想在部署到 tomcat 上时覆盖我的属性。我决定将文件放入/lib 文件夹,因为它自动位于类路径中,但我没有找到更好的方法(即特定于应用程序的类路径文件夹)

所以我可以将 application.properties 放在那里,只要我只有一个应用程序,它就可以工作。如果我有多个 spring boot 应用程序,我想命名文件(注意:特定于应用程序的命名)

  • 我可以使用应用程序。特定的 spring 配置文件并添加/lib/application-appname.properies,其中包括传递正确配置文件的必要性

  • 我可以使用 --spring.config.name,我不喜欢它,因为需要在命令行中传递它(我不知道在 war 文件中这样做的可能性,也许在 spring boot 主类中设置系统属性?)

  • 在@ConfigurationProperties(locations = "classpath:appname.properties, prefix..."它按我希望的方式工作,它被正确覆盖,但仅适用于我绑定(bind)值的类。

总结:有没有办法在类路径上复制特定命名的文件,从而覆盖应用程序属性????

已更新建议的解决方案有效我现在可以使用不同的属性名称,但我仍然无法在外部类路径中加载文件。

Adding [applicationConfig: [classpath:/my-app-realdb.properties]] PropertySource with search precedence immediately lower than [applicationConfigurationProperties]
Adding [applicationConfig: [classpath:/config/my-app.properties]] PropertySource with search precedence immediately lower than [applicationConfig: [classpath:/my-app-realdb.properties]]
Adding [applicationConfig: [classpath:/my-app.properties]] PropertySource with search precedence immediately lower than [applicationConfig: [classpath:/config/my-app.properties]]

最后一个在.war中,我想/lib/config文件夹应该优先

3. A classpath /config package
4. The classpath root

所以寻找答案仍在继续

更新2看起来顺序是

1. /config/application-<profile_specific>.properties
2. /application-<profile_specific>.properties
3. /config/application.properties
4. /application.properties

这意味着我不能覆盖,所有这些都具有非配置文件特定的属性,这可能是预期的,但对我来说,如果我只想要一个文件来统治它们并不是很有用。

最佳答案

您可以在主类中配置 spring.config.name 属性,可以在作为可执行存档运行时的 main 方法中配置,也可以在 configure 中配置 部署为 war 时的方法。例如:

@SpringApplication
public class SampleApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleApplication.class)
.properties("spring.config.name:my-app");
}

public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(SampleApplication.class)
.properties("spring.config.name:my-app").build().run(args);
}

}

有了这个更改,Boot 将查找名为 my-app.propertiesmy-app-{profile}.properties 的文件。

关于Spring Boot 应用程序特定的外部属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28318297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com