Interface WebApplicationInitializer
- All Known Implementing Classes:
AbstractAnnotationConfigDispatcherServletInitializer,AbstractContextLoaderInitializer,AbstractDispatcherServletInitializer
public interface WebApplicationInitializer
Interface to be implemented in Servlet 3.0+ environments in order to configure theServletContextprogrammatically -- as opposed to (or possibly in conjunction with) the traditionalweb.xml-based approach.Implementations of this SPI will be detected automatically by
SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container. See its Javadoc for details on this bootstrapping mechanism.Example
The traditional, XML-based approach
Most Spring users building a web application will need to register Spring'sDispatcherServlet. For reference, in WEB-INF/web.xml, this would typically be done as follows:<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>The code-based approach with
Here is the equivalentWebApplicationInitializerDispatcherServletregistration logic,WebApplicationInitializer-style:public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { XmlWebApplicationContext appContext = new XmlWebApplicationContext(); appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }As an alternative to the above, you can also extend fromAbstractDispatcherServletInitializer. As you can see, thanks to Servlet 3.0's newServletContext.addServlet(java.lang.String, java.lang.String)method we're actually registering an instance of theDispatcherServlet, and this means that theDispatcherServletcan now be treated like any other object -- receiving constructor injection of its application context in this case.This style is both simpler and more concise. There is no concern for dealing with init-params, etc, just normal JavaBean-style properties and constructor arguments. You are free to create and work with your Spring application contexts as necessary before injecting them into the
DispatcherServlet.Most major Spring Web components have been updated to support this style of registration. You'll find that
DispatcherServlet,FrameworkServlet,ContextLoaderListenerandDelegatingFilterProxyall now support constructor arguments. Even if a component (e.g. non-Spring, other third party) has not been specifically updated for use withinWebApplicationInitializers, they still may be used in any case. The Servlet 3.0ServletContextAPI allows for setting init-params, context-params, etc programmatically.A 100% code-based approach to configuration
In the example above,WEB-INF/web.xmlwas successfully replaced with code in the form of aWebApplicationInitializer, but the actualdispatcher-config.xmlSpring configuration remained XML-based.WebApplicationInitializeris a perfect fit for use with Spring's code-based@Configurationclasses. See @ConfigurationJavadoc for complete details, but the following example demonstrates refactoring to use Spring'sAnnotationConfigWebApplicationContextin lieu ofXmlWebApplicationContext, and user-defined@ConfigurationclassesAppConfigandDispatcherConfiginstead of Spring XML files. This example also goes a bit beyond those above to demonstrate typical configuration of the 'root' application context and registration of theContextLoaderListener:public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.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(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }As an alternative to the above, you can also extend fromAbstractAnnotationConfigDispatcherServletInitializer. Remember thatWebApplicationInitializerimplementations are detected automatically -- so you are free to package them within your application as you see fit.Ordering
WebApplicationInitializerexecutionWebApplicationInitializerimplementations may optionally be annotated at the class level with Spring's @Orderannotation or may implement Spring'sOrderedinterface. If so, the initializers will be ordered prior to invocation. This provides a mechanism for users to ensure the order in which servlet container initialization occurs. Use of this feature is expected to be rare, as typical applications will likely centralize all container initialization within a singleWebApplicationInitializer.Caveats
web.xml versioning
WEB-INF/web.xmlandWebApplicationInitializeruse are not mutually exclusive; for example, web.xml can register one servlet, and aWebApplicationInitializercan register another. An initializer can even modify registrations performed inweb.xmlthrough methods such asServletContext.getServletRegistration(String). However, ifWEB-INF/web.xmlis present in the application, itsversionattribute must be set to "3.0" or greater, otherwiseServletContainerInitializerbootstrapping will be ignored by the servlet container.Mapping to '/' under Tomcat
Apache Tomcat maps its internal
DefaultServletto "/", and on Tomcat versions <= 7.0.14, this servlet mapping cannot be overridden programmatically. 7.0.15 fixes this issue. Overriding the "/" servlet mapping has also been tested successfully under GlassFish 3.1.- Since:
- 3.1
- Author:
- Chris Beams
- See Also:
SpringServletContainerInitializer,AbstractContextLoaderInitializer,AbstractDispatcherServletInitializer,AbstractAnnotationConfigDispatcherServletInitializer
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidonStartup(ServletContext servletContext)Configure the givenServletContextwith any servlets, filters, listeners context-params and attributes necessary for initializing this web application.
Method Detail
onStartup
void onStartup(ServletContext servletContext) throws ServletException
Configure the givenServletContextwith any servlets, filters, listeners context-params and attributes necessary for initializing this web application. See examples above.- Parameters:
servletContext- theServletContextto initialize- Throws:
ServletException- if any call against the givenServletContextthrows aServletException