SPRINGMVC in Web. xml or how spring starts

Source: Internet
Author: User

Methods in the Contextloaderlistener class, the servlet container instantiates the Contextloaderlistener and calls this method when it is started, The most critical method is Createwebapplicationcontext, and the logic is that if ServletContext does not specify which applicationcontext to instantiate in the configuration file with the parameter Contextclass,
Then read the Org.springframework.web.context.support.XmlWebApplicationContext class in the Contextloader.properties file in the package where the Contextloader class is located and instantiate, and then servletcontext the SetAttribute method,
Org.springframework.web.context.WebApplicationContext.ROOT do key, instantiate the context to do value deposit.
Note that after instantiation, you must refresh the context again. This will take the value of the Contextconfiglocation field, which is the global configuration file, as the value of setconfiglocation. So in fact, when the configuration, Dispatchservlet should be equipped with springmvc.xml, and the global configuration file should not include Springmvc.xml, otherwise the
The bean will be instantiated two times, the parent factory one, the son factory one, of course, the child factory will not go to the parent factory to find, but after all, caused the waste of resources.
Contextloaderlistener mainly did this one thing, the rest is Dispatcherservlet do
@Override Public voidcontextinitialized (Servletcontextevent event) {Initwebapplicationcontext (Event.getservletcontext ()); }

Dispatcherservlet inherits from the parent class Httpservletbean (without rewriting) the Init () method, which mainly calls Initservletbean (), which is implemented in the parent class Frameworkservlet, mainly these two sentences

this. Webapplicationcontext = initwebapplicationcontext ();

Take out the rootcontext just deposited in the ServletContext, Dispatchservlet, according to the servlet specification, is configured in Web. XML,

If you are in the configuration and then inject a single servlet's own webapplicationcontext according to the construction parameters, then set the Rootcontext to parent

If not, see if the ContextAttribute parameter has no configuration, if any, instantiate this one.

If not, then there is a default class name in Dispatchservlet, take it out, create it yourself, and then contextconfiglocation the value of the field in your profile setconfiglocation, All the beans in the newly created child Spring Factory are taken from Contextconfiglocation.

These three cases will eventually refresh the factory, which is certain.

Refresh???

Springmvc in Web. Xml or how spring is started

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.