I am trying to execute below code and getting some error.
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AspectConfig.class);
try{
ctx.refresh();
}catch(Exception e){
ctx.close();
throw new RuntimeException("Bean refresh failed with exception:" + e.getMessage());
}
My AspectConfig.java
looks like
@EnableWebMvc
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages={"ac","ab","az"})
public class AspectConfig {
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
So the main thing here is I am using spring mvc call to call the code snippet. My spring WebServletConfiguration
is as follows
@Override
public void onStartup(ServletContext ctx) throws ServletException {
AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
webCtx.register(AppConfig.class);
webCtx.setServletContext(ctx);
ServletRegistration.Dynamic servlet = ctx.addServlet("dispatcher", new DispatcherServlet(webCtx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
And my AppConfig
is nearly the same as AspectConfig
except that it contains definition for ViewResolver
.
The error is
Exception in thread "pool-1-thread-2" java.lang.RuntimeException: Bean refresh failed with exception:Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
at framework.AnnotationProcessor.process(AnnotationProcessor.java:40)
at runners.MultiTestRunnable.run(MultiTestRunnable.java:29)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I have had a look at the links below but none seems to be working out. Is it that I cannot use the AnnotationContext
with Springs MVC
Below are the links I had a look