忽略从SpringBootApplication加载XML文件

时间:2018-08-27 09:33:56

标签: spring-boot spring-boot-test

我有一个像这样的Spring Boot应用程序结构:

src/main/java
            /main/Application.java
src/main/resource/
            /application-context.xml
src/test/java
            /main/TestApplication.java          
src/test/resource/
            /application-context-test.xml

当我通过Run As Spring Boot ApplicationRun as Junit开始申请时 application-context.xmlapplication-context-test.xml均已加载。

但是我只想在运行模式下加载application-context.xml,在测试模式下加载application-context-test.xml

初始化程序如下:

@ImportResource("classpath:application-context.xml")
public class Application{

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}


@ImportResource("classpath:application-context-test.xml")
public class TestApplication{

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }

}

但是它们不起作用。 两种XML均已加载。我该如何解决?

1 个答案:

答案 0 :(得分:0)

用:注释TestApplication:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class)