通过创建缓存bean工厂,最终解决了slow autowire by type problem问题。
我真的希望能够结合使用这样的CachingByTypeBeanFactory和SpringJUnit4ClassRunner来运行@Autowired的JUnit测试。但是似乎不可能通过ContextLoader在应用程序上下文中更改Bean Factory。
有没有其他方法可以做到这一点?
发布于 2011-01-07 21:57:10
创建您自己的ContextLoader,并将此注释附加到您的JUnit类:
@ContextConfiguration(loader=YourLoader.class)这是我的示例加载器,它实例化另一个或自定义的ApplicationContext,后者可以使用自定义的BeanFactory进行初始化(取决于功能):
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 "";
}}
在上述情况下,应用程序上下文(由Spring Framework提供)具有构造函数:
public GenericWebApplicationContext(DefaultListableBeanFactory beanFactory) {
super(beanFactory);
}https://stackoverflow.com/questions/4626051
复制相似问题