【问题标题】:Spring boot application specific external propertiesSpring Boot 应用程序特定的外部属性
【发布时间】:2015-04-03 19:02:03
【问题描述】:

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

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

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

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

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

  • 在 @ConfigurationProperties(locations = "classpath:appname.properties, prefix..." 中,它可以按照我想要的方式正常工作,但仅适用于我绑定值的类。

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

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

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 tomcat properties spring-boot


    【解决方案1】:

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

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

    【讨论】:

    • 这和我发现的结果一模一样,反正我喜欢这个语法 properties("spring.config.name:my-app") 比通过 Maps 多,谢谢你!!
    • 奇怪的是,它无法从 /lib/my-app.properties 读取 my-app.properties,我也尝试过 application.properties。没有。也许这个解决方案破坏了一些东西:-/
    • 我怀疑你误解了 Tomcat 的类加载是如何工作的。如果您的 war 文件中有 my-app.properties 文件,则在 Tomcat 的 lib 目录中放置一个同名文件不会覆盖它,因为 Tomcat 会首先查看 war 文件,并且只有在找不到任何内容时才会查看 lib在战争中。您可以通过在 Web 应用程序类加载器上设置 delegate=true 来更改此行为并使其首先在 lib 中查找。请参阅tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html 了解更多信息。
    • 因此,如果您想在外部使用 application.properties(或重命名的版本),唯一的解决方案是:不使用配置文件特定文件,我的意思是即使在您的可部署文件中。无论如何我都会接受上面的答案,因为它为我解决了大部分问题。
    猜你喜欢
    • 2022-01-22
    • 2015-01-23
    • 2019-03-02
    • 2018-02-25
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2019-06-12
    • 2012-01-06
    相关资源
    最近更新 更多