按照说明操作 here ,但我收到无法 Autowiring WebApplicationContext 的错误。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext-test.xml")
@WebAppConfiguration
public class AjaxTest {
@Autowired
private WebApplicationContext webApplicationContext; //FAILS
但是这个编译:
@Autowired
ServletContext servletContext;
private WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
我不明白为什么。
编辑 它使用 maven 运行良好,是我的编辑器 intellij 显示不正确的自动编译消息,bug in fact .
最佳答案
你的测试类应该实现ApplicationContextAware
接口(interface):
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
Spring 会自动注入(inject)应用上下文。
关于java - 在我的 spring 3.2 测试中 Autowiring webapplicationcotnext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004183/