0

按照此处的说明进行操作,但出现无法自动装配 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 显示不正确的自动编译消息,实际上是一个错误

4

1 回答 1

1

Your test class should implement 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 will automatically inject the application context.

于 2012-12-22T15:56:44.990 回答