My Application Could not open ServletContext resource

前端 未结 5 630
梦谈多话
梦谈多话 2020-12-01 01:52

i have Eclipse Maven web project where i use Spring mvc and Spring security. When i truy to launch it, it doesn\'t suceed to intialize the context

5条回答
  •  误落风尘
    2020-12-01 02:50

    If you are getting this error with a Java configuration, it is usually because you forget to pass in the application context to the DispatcherServlet constructor:

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebConfig.class);
    
    ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", 
        new DispatcherServlet()); // <-- no constructor args!
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/*");
    

    Fix it by adding the context as the constructor arg:

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebConfig.class);
    
    ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", 
        new DispatcherServlet(ctx)); // <-- hooray! Spring doesn't look for XML files!
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/*");
    

提交回复
热议问题