【问题标题】:How to load all .yml files from resources directory at the time of application startup in spring?如何在春季应用程序启动时从资源目录加载所有.yml文件?
【发布时间】:2023-03-24 05:35:01
【问题描述】:

我在 spring 应用程序的资源目录中有 2-3 个 .yml 文件。 我想在应用程序启动时自动加载所有这些文件。

我尝试了下面的代码,但没有成功。

ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(YamlLoadApplication.class)
                .properties("spring.config.name:applicationTest,CountriesData",
                        "spring.config.location:src/main/resources/")
                .build().run(args);

        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        MutablePropertySources sources = environment.getPropertySources();

请帮我解决这个问题。实现这一目标的最佳方法是什么?我将在整个应用程序中使用所有这些 yml 文件值。

谢谢

【问题讨论】:

    标签: spring spring-boot yaml


    【解决方案1】:

    您可以使用@PropertySource 注释将您的配置外部化到属性文件中。

    Spring 建议使用 Environment 来获取属性值。

    env.getProperty("mongodb.db");
    

    您可以提及在类中使用的属性文件。

    @Configuration
        @PropertySource({
            "classpath:config.properties",
            "classpath:db.properties"
        })
        public class AppConfig {
            @Autowired
            Environment env;
        }
    

    从 Spring 4 开始,您可以通过 ignoreResourceNotFound 忽略未找到的属性文件

      @PropertySources({
            @PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true),
            @PropertySource("classpath:config.properties")
    

    示例来自文章 - https://www.mkyong.com/spring/spring-propertysources-example/

    如果您需要更多信息,请参阅文章 From Spring documentation

    【讨论】:

      【解决方案2】:

      这里有一个问题"spring.config.location:src/main/resources/"spring.config.location 设置为src/main/resources/,这不是类路径资源而是文件系统资源。这是从您运行 Spring Boot 应用程序的当前目录中查找的。​​p>

      几个修复:

      像这样指定一个完整的文件系统路径:

      public static void main(String[] args) {
          ConfigurableApplicationContext applicationContext;
          applicationContext = new SpringApplicationBuilder(YmlsApplication.class)
              .properties("spring.config.name:applicationTest,CountriesData",
                  "spring.config.location:/Users/msimons/tmp/configs/")
              .build().run(args);
      
          ConfigurableEnvironment environment = applicationContext.getEnvironment();
          MutablePropertySources sources = environment.getPropertySources();
          sources.forEach(p -> System.out.println(p.getName()));
      }
      

      或者指定一个类路径资源。请注意,我将配置文件放在一个单独的目录下,该目录位于src/main/resources/custom-config

      public static void main(String[] args) {
          ConfigurableApplicationContext applicationContext;
          applicationContext = new SpringApplicationBuilder(YmlsApplication.class)
              .properties("spring.config.name:applicationTest,CountriesData",
                  "spring.config.location:classpath:/custom-config/")
              .build().run(args);
      
          ConfigurableEnvironment environment = applicationContext.getEnvironment();
          MutablePropertySources sources = environment.getPropertySources();
          sources.forEach(p -> System.out.println(p.getName()));
      }
      

      注意路径内的classpath:,并在资源的根级别以/ 启动目录。

      【讨论】:

      • 谢谢迈克尔。它现在对我有用。还有其他方法可以做到这一点吗?
      • 您可以将它们定义为命令行参数,即java -jar target/ymls-0.0.1-SNAPSHOT.jar --spring.config.name=CountriesData --spring.config.location=/Users/msimons/tmp/configs/ 或环境参数SPRING_CONFIG_NAME=CountriesData SPRING_CONFIG_LOCATION=/Users/msimons/tmp/configs/ ./mvnw spring-boot:run 请注意,还有spring.config.additional-location。与location 相比,它添加了位置并且不会替换默认位置。
      猜你喜欢
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 2018-06-15
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      相关资源
      最近更新 更多