Webapplicationcontext initialization in Spring Contextloaderlistener

Source: Internet
Author: User
<span id="Label3"></p><p><p>In spring and structs2 integration, you need to set up a listener in the Web. XML file, as follows</p></p><p><p></p></p><pre class="brush:xml;toolbar:false"><pre class="brush:xml;toolbar:false"><listener> <listener-class> Org.springframework.web.context.ContextLoaderListener < ;/listener-class> </listener></pre></pre><p><p>Contextloaderlistener is initialized when the Web container is initialized, and then initializes the Webapplicationcontext Work. Let's take a look at the key code for Contextloaderlistener:</p></p><p><p></p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">public void contextinitialized (servletcontextevent event) {this.contextloader = Createcontextloader ();        if (this.contextloader = = Null) {this.contextloader = this;    } this.contextLoader.initWebApplicationContext (event.getservletcontext ()); }</pre></pre><p><p>This code implements the Webapplicationcontext initialization, calling the Contextloader Initwebapplicationcontext method in the last line of the code, Let's take a look at the key code of Contextloader's Initwebapplicationcontext method:</p></p><pre class="brush:java;toolbar:false">    public webapplicationcontext initwebapplicationcontext (ServletContext  Servletcontext)  {        if  (servletcontext.getattribute ( Webapplicationcontext.root_web_application_context_attribute)  != null)  {             throw new illegalstateexception (                      " cannot initialize context because there is already a root  application context present -  " +                      "check whether you have  multiple contextloader* definitions in your web.xml! ");         } &nBsp;      log logger = logfactory.getlog (ContextLoader.class);         servletcontext.log ("initializing spring root  Webapplicationcontext ");        if  (logger.isInfoEnabled ())   {            logger.info ("Root  webapplicationcontext: initialization started ");         }         long starttime = system.currenttimemillis ();         try {             // store context in local instance variable, to  guarantee that            // it  is available on servletcontext shutdown.            if  ( This.context == null)  {                 this.context = createwebapplicationcontext (servletContext);             }</pre><p><p> The last line, and the Createwebapplicationcontext method is called, Let's look at the code for this method: </p> </p><pre class="brush:java;toolbar:false">    protected webapplicationcontext createwebapplicationcontext (ServletContext  sc)  {        Class<?> contextClass =  Determinecontextclass (sc);        if  (! ConfigurableWebApplicationContext.class.isAssignableFrom (CONTEXTCLASS))  {             throw new applicationcontextexception ("Custom context  class [" + contextclass.getname ()  +                      "] is not of type  [" + configurablewebapplicationcontext.class.getname ()  + "] ");         }        return  ( Configurablewebapplicationcontext)  beanutils.instantiateclasS (contextclass);     } </pre><p><p>The code shows that you return a configurablewebapplicationcontext here, and then take a look at the most critical code in the Contextloader initwebapplicationcontext method:</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">Servletcontext.setattribute (webapplicationcontext.root_web_application_context_attribute, this.context);</pre></pre><p><p>Here the context is deposited into the servletcontext, so later to use the Webapplicationcontext can be removed from the Servletcontext.</p></p><p><p>This article is from the "flying fish technology" blog, Please be sure to keep this source http://flyingfish.blog.51cto.com/9580339/1681334</p></p><p><p>Webapplicationcontext initialization in Spring Contextloaderlistener</p></p></span>
Related Article

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.