【问题标题】:Springboot - multiple property files per profileSpring Boot - 每个配置文件有多个属性文件
【发布时间】:2015-01-16 04:48:06
【问题描述】:

寻找在 Springboot 应用程序中配置多个 Profile 特定属性文件的最佳方法。下面是一个例子:

-资源
-- 应用程序.properties
-- 开发
-- 应用程序-dev.properties
-- ldap-dev.properties
--quartz-dev.properties
-- etc-dev.properties
-- 测试
-- 应用程序测试属性
-- ldap-test.properties
-- 石英测试.properties
-- etc-test.properties
-- 产品
-- 应用程序-prod.properties
-- ldap-prod.properties
-- 石英产品属性
-- etc-prod.properties

application.properties 和 application-profile.properties 文件加载正常。我正在寻找一种推荐的方法来加载其他配置文件特定的属性文件。我不确定是否有办法从基于配置文件的文件夹中加载所有属性文件?

【问题讨论】:

    标签: spring spring-boot spring-profiles


    【解决方案1】:

    我不确定是否有更好的方法或者我的建议是否真的有效,但你可以试试这个:

    在你的配置类之前添加@PropertySource注解

    @PropertySource("classpath:ldap-${spring.profiles.active}.properties", "classpath:quartz-${spring.profiles.active}.properties", "classpath:etc-${spring.profiles.active}.properties")
    

    要更好地了解 Spring 如何从不同来源和配置文件加载配置,请参阅 this 和 this。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      内置的配置监听器具有很大的灵活性。例如。您可以设置spring.config.name=application,ldap,quartz,etc 和spring.config.location=classpath:/,classpath:/dev,classpath:/prod,classpath:/test。或等效的环境变量。 Selim 答案中的链接记录了基本行为和配置选项。

      【讨论】:

        【解决方案3】:

        @PropertySources 可用于加载配置文件为的多个属性文件 ldap-${spring.profiles.active}.properties

        但如果您使用 YAML,@PropertySource 将不起作用。您必须使用@ConfigurationProperty 来加载除application.yml 以外的YML 文件

        【讨论】:

          【解决方案4】:

          从 Spring boot 2.0.4 开始,只要您在 spring.config.name 环境变量中指定配置文件,例如 spring.config.name=application,ldap,quartz,此功能即可开箱即用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-07-07
            • 2021-11-27
            • 1970-01-01
            • 2019-03-30
            • 2020-09-20
            • 1970-01-01
            • 2017-08-18
            • 2020-06-05
            相关资源
            最近更新 更多