简体   繁体   中英

Spring bean context refresh failing

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

Spring 5.0.0.M3 Error calling ApplicationEventListener : No ServletContext set - Exception encountered during context initialization

Error creating bean with name 'resourceHandlerMapping' defined in class path resource [EnableWebMvcConfiguration.class]

Why do you need to have two separate contexts? Can you add the @EnableAspectJAutoProxy annotation in the AppConfig class and try?

Or else if you need to have separate web and root contexts then I suggest that you try out AbstractAnnotationConfigDispatcherServletInitializer class for configuration, this can serve as a replacement for your WebServletConfiguration class. Please refer the link for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM