In the servlet 3.0+ environment, you can encode the servlet container to replace or combine the Web. xml file. Here is the registration Dispatcherservlet:
ImportOrg.springframework.web.WebApplicationInitializer; Public classMywebapplicationinitializerImplementsWebapplicationinitializer {@Override Public voidonstartup (ServletContext container) {Xmlwebapplicationcontext AppContext=NewXmlwebapplicationcontext (); Appcontext.setconfiglocation ("/web-inf/spring/dispatcher-config.xml"); Servletregistration.dynamic Registration= Container.addservlet ("Dispatcher",NewDispatcherservlet (AppContext)); Registration.setloadonstartup (1); Registration.addmapping ("/"); }}
Webapplicationinitializer is an interface provided by spring MVC that allows your implementation to be detected and automatically used to initialize any servlet 3 container. One of its abstract base class implementations is Abstractdispatcherservletinitializer, which makes it easier to register dispatcherservlet-just rewrite the appropriate methods, specify the servlet mappings, Specify the location of the Dispatcherservlet configuration.
This is recommended in the Java-based spring configuration:
Public classMywebappinitializerextends abstractannotationconfigdispatcherservletinitializer {@OverrideprotectedClass<?>[] getrootconfigclasses () {return NULL; } @OverrideprotectedClass<?>[] getservletconfigclasses () {return NewClass[] {mywebconfig.class }; } @Overrideprotectedstring[] Getservletmappings () {return NewString[] {"/" }; }}
If you use the XML-based spring configuration, you should inherit Abstractdispatcherservletinitializer directly (note that Java-based configuration is an inherited Abstractannotationconfigdispatcherservletinitializer):
Public classMywebappinitializerextends abstractdispatcherservletinitializer {@OverrideprotectedWebapplicationcontext Createrootapplicationcontext () {return NULL; } @OverrideprotectedWebapplicationcontext Createservletapplicationcontext () {Xmlwebapplicationcontext cxt=NewXmlwebapplicationcontext (); Cxt.setconfiglocation ("/web-inf/spring/dispatcher-config.xml"); returnCXT; } @Overrideprotectedstring[] Getservletmappings () {return NewString[] {"/" }; }}
Abstractdispatcherservletinitializer also provides a convenient way to add filter instances and have them automatically mapped to Dispatcherservlet (is the link added to the Dispatcherservlet map):
Public class extends Abstractdispatcherservletinitializer { // ... @Override protected filter[] Getservletfilters () { return New New New characterencodingfilter ()};} }
Note that each filter you add has a default name-based on its type, and is automatically mapped to Dispatcherservlet.
Abstractdispatcherservletinitializer's isasyncsupported protected method, Can be used to enable asynchronous support for Dispatcherservlet and all filters that are mapped to it. Default, Enabled, True.
Finally, if you need to further customize the Dispatcherservlet, you can rewrite the Createdispatcherservlet method.
Official Document Address:
Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-container-config
Spring 4 Official Document Learning (11) Coded servlet container initialization for WEB MVC framework