2016-08-14 673 views
1

我有兩個類:多個類與嵌套@Configuration失敗

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class BaseConfiguratorServiceTest { 

    @Configuration 
    @ImportResource("/application-context.xml") 
    static class ContextConfiguration { 
     @Bean 
     public BaseConfiguratorService baseConfiguratorService() { 
      return new BaseConfiguratorService(); 
     } 
    } 
} 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class AnotherTest { 

    @Configuration 
    @ImportResource("/application-context.xml") 
    static class AnotherConfiguration { 
     @Bean 
     public BaseConfiguratorService baseConfiguratorService() { 
      return new BaseConfiguratorService(); 
     } 
    } 
} 

我收到此錯誤信息:

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context. 
Offending resource: class path resource [application-context.xml] 

我不知道的原因。我在一次運行一個測試:

MVN測試-Dtest = BaseConfiguraionTest

然後作爲第二類環境未在運行時加載,所以應春關懷另一個類有此註釋?除此之外,Spring甚至還知道是否有另一個用@Configuration註解的類。

回答

0

我有同樣的問題,因爲你,雖然我仍然不知道爲什麼它會發生,我發現這個答案解決了我(我們)的問題:https://stackoverflow.com/a/39307385/1410035

我使用@WebAppConfiguration(其回答地址),但希望它仍然適用於您的情況。

我在春季4.2.x FWIW。此外,該鏈接的答案是來自Spring代碼的作者,所以我們可以相信它。