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

Zve*_*tko 1 spring tomcat properties spring-boot

我正在寻找解决这个问题的方法.

我有几个属性文件(application.properties,application-realdb.properties).假设我从"realdb"弹簧配置文件开始.现在我想在tomcat上部署时覆盖我的属性.我决定把文件放到/ lib文件夹中,因为它自动在classpath上,我没有找到更好的(即app特定的classpath文件夹)

所以我可以放置application.properties,只要我只有一个应用程序就可以工作.如果我有多个spring启动应用程序,我想命名该文件(注意:特定于应用程序的命名)

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

  • 我可以使用--spring.config.name,我不喜欢在命令行中传递它的需要(我不知道在war文件中可以做到这一点,也许在春天设置系统属性)启动主类?)

  • 在@ConfigurationProperties中(locations ="classpath:appname.properties,prefix ..."它可以正常工作,但它只适用于我正在绑定值的类.

总结:有没有办法如何在类路径上复制特定命名的文件,从而覆盖application.properties ????

UPDATED 建议的解决方案工作我现在可以使用不同的属性名称,但我仍然有问题在classpath外加载文件.

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]]
Run Code Online (Sandbox Code Playgroud)

最后一个在.war中,我认为/ lib/config文件夹应该具有优先权

3. A classpath /config package
4. The classpath root
Run Code Online (Sandbox Code Playgroud)

所以寻找答案仍在继续

UPDATE2 看起来像订单

1. /config/application-<profile_specific>.properties
2. /application-<profile_specific>.properties
3. /config/application.properties
4. /application.properties
Run Code Online (Sandbox Code Playgroud)

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

And*_*son 7

您可以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);
    }

}
Run Code Online (Sandbox Code Playgroud)

通过此更改,Boot将查找名为my-app.properties和的文件my-app-{profile}.properties.