"Spring Framework"? Webapplicationinitializer Source code Analysis and application

Source: Internet
Author: User

Tag: Color description operation Cat ring is larger than the link parameter file

1. Background

In large environments where the convention is larger than the configuration, the XML configuration file is increasingly superseded by the 0 configuration. Most of the configuration in the original XML is also defined in the Java class.

Instead, Webapplicationinitializer can be seen as a substitute for Web. XML, which loads the implementation class when it is launched, and implements the operation of the servlet, filter, listener in the implementation class.

2. Source Code Analysis

First look at the Webapplicationinitializer interface definition, you can see that there is only one interface, parameter ServletContext explained to be used to initialize;

Again, see the annotations on the class, the hint can refer to springservletcontainerinitializer.

  

  turn to see Springservletcontainerinitializer, first look at class definition;

First, the class implements the Servletcontainerinitialize interface, and it is explicitly stated in the comments that the implementation class is loaded when the container starts.

  

Then look at the rewritten method Onstartup (set<class<?>> webappinitializerclasses, ServletContext ServletContext)

  webappinitializerclasses, which is interpreted as the implementation class for all Webapplicationinitializer interfaces, that is, when the container starts, The implementation class type of webappinitializerclasses is passed as a parameter;

The following code is easy to read, if it is not the interface, put the implementation of the class instantiation to LinkedList, and then loop through the implementation of the method overridden in the class to achieve the role of XML instead.

  

3. Application

After analyzing the source code, the application of the actual project.

Nothing to say, write the servlet, filter, listener operation.

"Spring Framework"? Webapplicationinitializer Source code Analysis and application

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.