Java 在我的spring 3.2测试中自动连接WebApplicationContext

Java 在我的spring 3.2测试中自动连接WebApplicationContext,java,spring,testing,web-applications,Java,Spring,Testing,Web Applications,按照说明进行操作,但我得到的错误是无法自动连接WebApplicationContext @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("applicationContext-test.xml") @WebAppConfiguration public class AjaxTest { @Autowired private WebApplicationContext webApplicationContext; //

按照说明进行操作,但我得到的错误是无法自动连接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显示了错误的自动编译消息,一个。

您的测试类应该实现
ApplicationContextAware
接口:

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将自动注入应用程序上下文。

好的,@ContextConfiguration注释不就是应该这样做的吗?它们在示例中没有显示实现该接口的情况,或者我遗漏了什么吗?不,您不需要在Spring 3.2中这样做,用stacktrace@hoaz好啊我认为这是我的测试应用程序上下文无法访问的问题load@hoaz实际上,这运行正常,这是一个intellij错误。您能提供异常吗?@null我提供了,无法自动连接WebApplicationContext