Skip to content

Instantly share code, notes, and snippets.

@jbrackett
Created December 12, 2012 02:11
Show Gist options
  • Save jbrackett/4264287 to your computer and use it in GitHub Desktop.
Save jbrackett/4264287 to your computer and use it in GitHub Desktop.
Basic Servlet initializer for Spring to replace web.xml in Servlet 3 containers (Tomcat 7, Jetty 8, etc.)
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class ServletInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
//Register any @Configuration classes. Could use xml instead but that's no fun.
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class, WebConfig.class);
//Create and configure the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
new DispatcherServlet(ctx));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment