2011-01-07 5 views
8

slow autowire by type problemは、最後にキャッシングBeanファクトリを作成することで解決されました。SpringJUnit4ClassRunnerでBeanFactoryをカスタマイズしますか?

@AutowiredでJUnitテストを実行するために、SpringJUnit4ClassRunnerと一緒にこのようなCachingByTypeBeanFactoryを使用することが本当に好きです。しかし、ContextLoaderを介してアプリケーションコンテキストでBean Factoryを変更することはできないようです。

これを行う方法は他にありますか?

答えて

8

独自のContextLoaderを作成し、JUnitのクラスにこの注釈を付ける:

@ContextConfiguration(loader=YourLoader.class) 

これは、順番に、カスタムたBeanFactory(能力に依存)で初期化されることができる別のまたはカスタムのApplicationContextをインスタンス化する私の例ローダーです: (スプリングフレームワークによって提供される)上記の場合アプリケーションのコンテキストにおいて

public class XmlWebApplicationContextLoader extends AbstractContextLoader { 

    public final ConfigurableApplicationContext loadContext(final String... locations) throws Exception { 
     ServletContext servletContext = new MockServletContext("war", new FileSystemResourceLoader()); 
     GenericWebApplicationContext webContext = new GenericWebApplicationContext(); 
     servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext); 
     webContext.setServletContext(servletContext); 
     new XmlBeanDefinitionReader(webContext).loadBeanDefinitions(locations);   
     AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext); 
     webContext.refresh(); 
     webContext.registerShutdownHook(); 
     return webContext; 
    } 

    protected String getResourceSuffix() { 
     return ""; 
    } 

}

コンストラクタを有します。

public GenericWebApplicationContext(DefaultListableBeanFactory beanFactory) { 
    super(beanFactory); 
} 
+0

これは正確にどのように役立ちますか? ContextLoaderのSpringソースコードを読んでいて、BeanFactoryを受け入れることができないようです。そしてAFIK ContextLoader!= BeanFactory – krosenvold

+0

あなたはどんなアプリケーションコンテキストタイプを使用していますか? GenericApplicationContextを新しいBFでインスタンス化し、カスタムローダのloadContextメソッドでそれを返すことができます。 – gertas

+0

私には見えます。 ContextLoaderは、文字列の配列をアプリケーションコンテキストに変換する方法を指定するインタフェースです。 – Pace

関連する問題