Spring Boot 应用程序特定的外部属性 [英] Spring boot application specific external properties

查看:64
本文介绍了Spring Boot 应用程序特定的外部属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为这个问题寻求解决方案.

I'm seeking solution for this problem.

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

I have several property files(application.properties, application-realdb.properties). Let's say I'm starting with "realdb" spring profile. Now I want to override my properties when deployed on tomcat. I decided to put the file to /lib folder as it is automatically on classpath and I didn't found any better(ie app specific classpath folder)

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

So I can place there application.properties, which will work so long as I will have only one application. In case I have multiple spring boot application I'd like to name the file(NOTE: application specific naming)

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

  • I can use app. specific spring profile and add /lib/application-appname.properies, which includes the necessity to pass the proper profile

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

I can use --spring.config.name, which I don't like as well for the need of passing it in command line(I'm not aware of possibility to do it in the war file, maybe set system property in spring boot main class?)

在@ConfigurationProperties(locations = "classpath:appname.properties, prefix..." 它按我的意愿工作,它被正确覆盖,但仅适用于我绑定值的类.

in @ConfigurationProperties(locations = "classpath:appname.properties, prefix..." it works as I want it is overriden properly, but only for the class I'm binding the values.

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

Sum up: Is there a way how to copy specificly named file on classpath and, thus override application.properties????

更新建议的解决方案有效我现在可以使用不同的属性名称,但是在外部类路径中加载文件时仍然存在问题.

UPDATED Proposed solution works I can now use different properties name, but I still have problem with loading files in classpath outside.

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文件夹应该有优先权

the last one is in .war, I quess the /lib/config folder should have precedence

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

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

This means I cannot override, all with non-profile specific properties, which maybe is as intended, but for me, if I want just one file to rule them all not very useful.

推荐答案

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

You can configure the spring.config.name property in your main class, either in the main method when running as an executable archive or in the configure method when being deployed as a war. For example:

@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 的文件.

With this change in place Boot will look for files named my-app.properties and my-app-{profile}.properties.

这篇关于Spring Boot 应用程序特定的外部属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆