【问题标题】:@SpringBootTest - failed to load applicationcontext@SpringBootTest - 无法加载应用程序上下文
【发布时间】:2018-12-25 00:15:05
【问题描述】:

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class FileInterfaceTest {
 @Test
 public void contextLoads() {
 }

}

应用:

应用配置:

      @Configuration
      @ImportResource({ "classpath:process-flows.xml" })
      public class AppConfig {
               }

有 Bootstraploader 类。

错误:

       java.lang.IllegalStateException: Failed to load ApplicationContext
       Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [file.properties] cannot be opened because it does not exist

资源位置:

  src/main/resources
          ---process-flow.xml
          --- process.yml
  src/main/resources/env/cconfig
          --- file.properties

【问题讨论】:

    标签: spring-boot spring-boot-test spring-junit springjunit4classrunner


    【解决方案1】:

    您的文件夹结构中有错字:src/main/resources/env/cconfig 必须是 src/main/resources/env/config

    根据Spring Externalized Configuration 中的定义,SpringApplication 从以下位置的application.properties 文件中加载属性并将它们添加到 Spring 环境中:

    • 当前目录的/config子目录
    • 当前目录
    • 类路径/config
    • 类路径根

    【讨论】:

      【解决方案2】:

      除了“cconfig”文件夹名称(不确定这是否是拼写错误)我还看到您的 @ImportResource 被提及为 classpath:process-flows.xml 但是您的资源文件夹有名为 process-flow.xml 的文件

      如果这也不是拼写错误,请通过将 process-flow.xml 重命名为 process-flows.xml 添加一个“s”并重试

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-27
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-27
        相关资源
        最近更新 更多