2014-03-06 19 views
1

WebMvcConfigurationSupport [這個問題是類似「Preventing a @EnableWebMvc-annotated class from being picked up by @ComponentScan」,只能用不同的攻擊向量的問題,這是我在春天4有一個問題]編程register'ing代替@Configuration

由於@ComponentScan發現所有@Configuration實例,它也提取了我在servlet環境中所需的WebMvcConfigurationSupport的擴展。但在集成測試模式下,我不希望這樣,因爲它抱怨ServletContext不存在。

 
java.lang.IllegalStateException: Failed to load ApplicationContext 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) 
    at ... 
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling 
    at org.springframework.util.Assert.notNull(Assert.java:112) 
    at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.(DefaultServletHandlerConfigurer.java:54) 
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:329) 
    at ... 

我使用的而不是讓它通過@ComponentScan找到由也具有@Configuration批註解決這個問題,我直接包括使用AnnotationConfigWebApplicationContext.register(thatClass)它,這是不集成測試期間運行代碼建立。

這工作得很好,直到我升級到4.0.2春天,在那裏我得到一個4.0類CompositeUriComponentsContributor一個新的異常:

 
Caused by: java.lang.IllegalArgumentException: 'uriComponentsContributors' must not be null 
    at org.springframework.util.Assert.notNull(Assert.java:112) ~[spring-core-4.0.2.RELEASE.jar:4.0.2.RELEASE] 
    at org.springframework.web.method.support.CompositeUriComponentsContributor.(CompositeUriComponentsContributor.java:88) ~[spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE] 
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.mvcUriComponentsContributor(WebMvcConfigurationSupport.java:573) ~[spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_51] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_51] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_51] 
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_51] 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) ~[spring-beans-4.0.2.RELEASE.jar:4.0.2.RELEASE] 
    ... 34 common frames omitted 

因此很明顯,這出於某種原因,現在需要「@配置 - 找到「,而不是包含在註冊電話中。

這是爲什麼?我怎樣才能解決這個問題?我明白我可以將其移出@ComponentScan「路徑」,但我想知道會發生什麼。

+0

查看配置文件。 –

+0

@SotiriosDelimanolis:一個鏈接,以及評論 - 或者實際上是一個答案 - 將不勝感激! – stolsvik

回答

1

試試這個:用@WebAppConfiguration註釋你的集成測試類。這將爲Spring MVC設置創建一個模擬ServletContext