Error call 'refresh' before accessing beans via the ApplicationContext

Asked on August 09, 2014
Hi, I am using
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Configuration.class);
But got below error. What is wrong?
Caused by: java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
at com.concretepage.WebAppInitializer.onStartup(WebAppInitializer.java:17)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5423)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more

Replied on August 09, 2014
You should use
ctx.refresh();
as
ctx.setServletContext(servletContext);
ctx.refresh();
It will remove your error.
ctx.refresh();
as
ctx.setServletContext(servletContext);
ctx.refresh();
It will remove your error.