Spring 4 Official Document Learning (11) Coded servlet container initialization for WEB MVC framework

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.