Contextloaderlistener Effect of the explanation

Source: Internet
Author: User

Reference URL: http://blog.csdn.net/ysughw/article/details/8992322

the role of the Contextloaderlistener listener is to automatically assemble ApplicationContext configuration information when the Web container is started. because it implements the Servletcontextlistener interface, when the Web. XML configures this listener, when the container is started, it executes the method it implements by default. As for applicationcontext.xml where this configuration file is deployed, how to configure multiple XML files is now a way to view its API documentation. The Contextloader class is associated with Contextloaderlistener, so the entire load configuration process is done by Contextloader. Take a look at its API description.

Class Contextloader API Source:

Performs the actual initialization work for the root application context. Called by ContextLoaderListener .

Looks for a , "Contextclass"  parameter at the  Web. XML  context-param Level to specify the context class type, falling back to the default of  xmlwebapplicationcontext  if Not found. With the default Contextloader implementation, no context class specified needs to implement the Configurablewebapplicati Oncontext interface.

Processes a "contextConfigLocation" Context-param and passes its value to the context instance, parsing it into potentially multiple file pa Ths which can is separated by any number of commas and spaces, e.g. "Web-inf/applicationcontext1.xml, Web-inf/applicationc Ontext2.xml ". Ant-style path patterns is supported as well, e.g. "Web-inf/*context.xml,web-inf/spring*.xml" or "web-inf/**/* Context.xml ". If not explicitly specified, the context implementation was supposed to use a default location (with Xmlwebapplicationconte XT: "/web-inf/applicationcontext.xml").


The first paragraph illustrates the role of Contextloader-it assembles the request context for the initialization work, which is triggered by the call to Contextloaderlistener.

In the second paragraph, Contextloader will look for it in Web.contextClass参数,它是上下文的class类型,默认的是XmlWebApplicationContext。

类ContextLoader中相应源码:

... Public StaticFinal String Context_class_param ="Contextclass";...Private StaticFinal String Default_strategies_path ="contextloader.properties";...Static {        Try{Classpathresource Resource=NewClasspathresource ("contextloader.properties", Contextloader.class); Defaultstrategies=propertiesloaderutils.loadproperties (Resource); } Catch(IOException ex) {Throw NewIllegalStateException ("Could not load ' contextloader.properties ':"+ex.getmessage ()); } currentcontextperthread=NewConcurrenthashmap (1); }
Contextloader.properties
class  for contextloader.# used as fallback when no explicit context implementation have been specified as context-param . # meant to being customized by application developers.org.springframework.web.context.WebApplicationContext= Org.springframework.web.context.support.XmlWebApplicationContext

The third paragraph, how to deploy the ApplicationContext XML file.

If you do not write any parameter configuration information in Web. XML, the default path is/web-inf/applicationcontext.xml, and the name of the file created under the Web-inf directory must be applicationcontext.xml;

If you want to customize the file name, you can add contextconfiglocation to the context parameter in Web. XML:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/web-inf/classes/applicationcontext-*.xml
</param-value>
</context-param>
In <param-value> </param-value> Specify the appropriate XML file name, if there are multiple XML files, can be written together and one "," number separated. The applicationcontext-*.xml above uses a wildcard character, For example, in this directory there are applicationcontext-ibatis-base.xml,applicationcontext-action.xml,applicationcontext-ibatis-dao.xml and other files, will be loaded together.

Corresponding Source:

declarations in the class Contextloader:

 Public Static Final String Config_location_param = "Contextconfiglocation";

Because the default context for Contextloader is xmlwebapplicationcontext, the declaration in Xmlwebapplicationcontext:

 Public Static Final String default_config_location = "/web-inf/applicationcontext.xml";

This shows that the Applicationcontext.xml file location can have two default implementations:

The first kind: put it directly under/web-inf, and declare a listener in Web. xml;

The second kind: put it under Classpath, but add <context-param> to the Web. XML, and use it to indicate the location of your applicationcontext.xml to be loaded by the container. According to Struts2, the official files of spring are written as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>

Contextloaderlistener Effect of the explanation

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.