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