【问题标题】:Loading properties placed under different folders inside resources in Java加载Java中资源内不同文件夹下的属性
【发布时间】:2020-04-26 21:35:51
【问题描述】:

资源文件夹下的属性文件很少。

src/main/java
src/main/resources-
                  prod
                     -> application.properties
                  prod-brand-1
                     -> application.properties
                  prod-brand-2
                     -> application.properties
                  application-default.properties (directly placed under resources folder)

在我的一个 Java 类中,我正在初始化一个环境 bean,它将使用 @PropertySource 注释加载属性文件。

@Configuration
@PropertySource({"classpath:application-default.properties",
"classpath:prod/application.properties",
"classpath:prod-brand-1/application.properties",
"classpath:prod-brand-2/application.properties"
})
public class PropertyConfig{
//
}

我可以加载 application-default.properties,因为它直接放置在资源文件夹下,但不能加载那些放置在诸如 prod,prod-1,prod- 等目录下的那些2.

在实际场景中,需要将下面的内容替换为一些占位符,并且需要通过环境变量注入其中的值(prod,prod-brand-1,prod-brand-2)。

@PropertySource({"classpath:prod/application.properties" "classpath:prod-brand-1/application.properties" "classpath:prod-brand-2/application.properties"})

应该是这样的:-

@PropertySource({"classpath:{dir}/application.properties"})

请建议我如何将占位符与@PropertySource 一起使用。

【问题讨论】:

  • 不要。让 Spring Boot 通过您可以在启动时提供的 spring.config.additional-location 属性指定其他位置来加载它们。
  • 你怎么知道没有加载附加文件?我有一个类似的问题:stackoverflow.com/questions/59526999/…。附加文件的内容是什么?
  • @hotzst 我确实通过 Junit 测试对其进行了测试,并且由于它不存在而找不到错误文件“prod/application.properties”。当我删除这些 prod/application.properties,prod-brand-1/application.properties 并且只有 @PropertySource({"classpath:application-default.properties"}) 时,它工作正常并且环境中有 application.properties 文件.
  • @M.Deinum 我没有使用 SpringBoot :(

标签: java spring maven properties


【解决方案1】:

您可以对同一位置(资源文件夹)中的文件尝试这样

@PropertySources({
    @PropertySource("classpath:foo.properties"),
    @PropertySource("classpath:bar.properties")
})
public class PropertiesWithJavaConfig {
    //...
}

对于您所说的多个位置,您可以尝试这些代码

  @Configuration
    public class PropertiesConfiguration {
         @Bean
        public PropertyPlaceholderConfigurer properties() {
            final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    //        ppc.setIgnoreUnresolvablePlaceholders(true);
            ppc.setIgnoreResourceNotFound(true);

            final List<Resource> resourceLst = new ArrayList<Resource>();

            resourceLst.add(new ClassPathResource("myapp_base.properties"));
            resourceLst.add(new FileSystemResource("/etc/myapp/overriding.propertie"));
            resourceLst.add(new ClassPathResource("myapp_test.properties"));
            resourceLst.add(new ClassPathResource("myapp_developer_overrides.properties")); // for Developer debugging.

            ppc.setLocations(resourceLst.toArray(new Resource[]{}));

            return ppc;
        }

【讨论】:

  • 如果 foo.properties 直接放在资源文件夹下,它肯定会加载。我的情况是 - foo.properties 放在多个目录中,正如我提到的 - prod-brand-1,prod-brand- 2 .这些目录值应该在运行时通过环境变量解析
【解决方案2】:

您可以在与PropertyConfig.class 文件相同的包中声明您的属性源。 说 `src/main/java/com/mypackage/me/prod-brand-2/application.properties。

然后像这样声明你的属性来源:

package com.mypackage.me.prod-brand-2;

@Configuration
@PropertySource("classpath:/com/mypackage/me/prod-brand-2/application.properties")
public class PropertyConfig{
//
}`

或者使用新的@PropertySources(不要和@PropertySource混淆)注解:

【讨论】:

    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 2018-09-04
    • 2018-06-03
    • 2017-03-23
    • 2011-04-09
    • 2016-03-17
    • 1970-01-01
    • 2010-11-13
    相关资源
    最近更新 更多