Created
December 12, 2012 02:11
-
-
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.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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