Skip to content

Instantly share code, notes, and snippets.

@criminy
Created July 19, 2011 17:36
Show Gist options
  • Save criminy/1093205 to your computer and use it in GitHub Desktop.
Save criminy/1093205 to your computer and use it in GitHub Desktop.
Bootstraps a Servlet 3.0 system using Spring MVC, Spring Security, etc.
public class MyWebAppinitializer extends StandardWebAppInitializer {
//customize the creatino of the web application context
@Override
protected WebApplicationContext createWebApplicationContext() {
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("com.whatever");
root.getEnvironment().setDefaultProfiles("embedded");
return root;
}
protected void createFlashFilter(ServletContext sc)
{
//disable the flash filter without disabling the rest of the system
}
}
public abstract class StandardWebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext sc) throws ServletException
{
WebApplicationContext root = createWebApplicationContext();
createContextLoaderListener(sc, root);
createFlashFilter(sc);
createHttpMethodFilter(sc);
createSpringSecurityFilterChain(sc);
createAdditionalComponents(sc,root);
createServlet(sc, root);
}
protected void createFlashFilter(ServletContext sc)
{
// Allows attributes to be accessed on the next request
sc.addFilter("flashMapFilter", FlashMapFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
}
protected void createHttpMethodFilter(ServletContext sc)
{
// Enables support for DELETE and PUT request methods with web browser
// clients
sc.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
}
protected void createSpringSecurityFilterChain(ServletContext sc)
{
// Sets up Spring Security
sc.addFilter("springSecurityFilterChain",new DelegatingFilterProxy())
.addMappingForUrlPatterns(null,false,"/*");
}
protected void createServlet(ServletContext sc,WebApplicationContext root)
{
// Handles requests into the application
ServletRegistration.Dynamic appServlet = sc.addServlet("appServlet",
new DispatcherServlet(root));
appServlet.setLoadOnStartup(1);
Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException(
"'appServlet' could not be mapped to '/' due "
+ "to an existing mapping. This is a known issue under Tomcat versions "
+ "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
}
}
protected void createContextLoaderListener(ServletContext sc,WebApplicationContext root)
{
// Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root));
}
protected void createAdditionalComponents(ServletContext sc,WebApplicationContext root)
{
// does nothing
}
/**
* Creates the web application context backing this Web application.
* @return
*/
protected abstract WebApplicationContext createWebApplicationContext();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment