簡體   English   中英

Spring應用程序上下文 - 主文件夾和測試文件夾

[英]Spring Application Context - Main and Test Folders

我有一個Spring項目,有4個典型的源文件夾 - main/srcmain/resourcestest/srctest/resources 當我運行我的應用程序時,Spring在main/resources獲取應用程序上下文文件,如果我運行任何Junit測試,它將獲取test/resources下的application-context.xml文件。 Spring如何適當地獲取application-context.xml文件或者是否涉及任何配置?

嘗試使用jsfStruts運行任何其他項目,他們也將從相應的文件夾中選擇資源。 它與春天無關。 它將由maven或您正在使用的任何其他構建系統處理。

main/src , main/resources , test/src , test/resources 

創建maven或gradle項目時,這些文件夾是標准的。

應用程序告訴Spring應用程序上下文的位置。 Web應用程序通過在web.xml中配置ContextLoaderListener來完成此操作。 對於每個測試,如何加載應用程序上下文是測試配置的一部分,@ ContextConfiguration注釋指定如何從哪個位置或從帶注釋的類加載上下文。

例如,如果我設置要使用的測試

@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
classes = MyTest.ContextConfiguration.class)
public class MyTest {
    @Autowired MyStuff stuff;

    static class ContextConfiguration {
        @Bean public MyStuff getMyStuff() {
            return new MyStuff();
        }
    }
}

然后MyTest使用測試中的注釋來決定注入的內容,並使用指定的ContextConfiguration填充這些字段。 它完全忽略了類路徑中的任何xml配置。

上下文加載器還可以指定加載上下文的位置,請參閱org.springframework.test.context.ContextLoader的文檔。

你沒有說你正在使用的是什么版本的Spring。 在3.0測試之前,通過實現類org.springframework.test.AbstractSpringContextTests的抽象方法loadContext來管理測試上下文,這是Spring感知測試擴展的層次結構的一部分。

我知道這個問題很老但是使用Spring-boot 2.x文件夾結構是不同的。 這是我從Spring初始化器和IntelliJ得到的:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM