Skip to content

Instantly share code, notes, and snippets.

@maggandalf
Forked from anonymous/WebAppInitializer.java
Created June 20, 2011 09:04
Show Gist options
  • Save maggandalf/1035340 to your computer and use it in GitHub Desktop.
Save maggandalf/1035340 to your computer and use it in GitHub Desktop.
Java-based conatiner configuration with no web.xml.
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RootContextConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(mytld.mycompany.myapp.config.appServlet.ServletContextConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet(
"appServlet", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/main");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment