Skip to content

Instantly share code, notes, and snippets.

@keesun
Created November 24, 2011 05:10
Show Gist options
  • Save keesun/1390672 to your computer and use it in GitHub Desktop.
Save keesun/1390672 to your computer and use it in GitHub Desktop.
Spring 3.1's MVC Web Application Initializer
public class WebAppIntializer implements WebApplicationInitializer {
 
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //parent
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(AppConfig.class);
 
        servletContext.addListener(new ContextLoaderListener(rootContext));
//        new ContextLoader(rootContext).initWebApplicationContext(servletContext);
 
        //child
        AnnotationConfigWebApplicationContext dispatcherServletContext = new AnnotationConfigWebApplicationContext();
        dispatcherServletContext.register(WebConfig.class);
 
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring", new DispatcherServlet(dispatcherServletContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment