The role of contextconfiglocation in spring in Web. xml

Source: Internet
Author: User

I. How spring uses multiple XML configuration files

1, define the Contextconfiglocation parameter in Web. XML, spring will use this parameter to load all the comma-delimited files, if not, spring default load web-inf/ Applicationcontext.xml file.

For example:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>         classpath*:conf/spring/applicationcontext_core*. xml,        classpath*:conf/spring/ applicationcontext_dict*. xml,        classpath*:conf/spring/applicationcontext_hibernate.xml,    

  The <param-value> of the Contextconfiglocation parameter defines the Spring configuration file to be loaded.

principle: Using Servletcontextlistener Implementation

  Spring provides an implementation class Contextloaderlistener for Servletcontextlistener that can be used as listener, which is automatically found at creation time web-inf/ Under the Applicationcontext.xrnl file. Therefore, if there is only one configuration file and the file name is Applicationcontext.xml, simply add the following code to the Web. xml file:

<listener> <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class></listener>

  If more than one configuration file needs to be loaded, consider using the <context-param> element to determine the file name of the configuration file. Because Contextloaderlistener is loaded, a parameter named Contextconfiglocation is found. Therefore, the configuration <context-param> time parameter name should be contextconfiglocation.

The Web. xml file with multiple configuration files is as follows:

 <1--XML file " <web-app>  <! determine multiple configuration files;  <context-param> <1--   < param-name>contextconfiglocation</param-name> <!   More than one configuration file, separated by two   <param-value>/  web-nf/daocontext.xml./web-inf/applicationcontext.xml  </param-value> </context-param>  <!--use listener to create ApplicationContext instance-->  < listener> <listener-class  > Org.springframework.web.context.contextloaderlistener</listener-class  > </listener></web-app> 

 If the configuration file is not contextconfiglocation specified, spring automatically finds the Applicationcontext.xml configuration file. If there is a contextconfiglocation, the configuration file determined by this parameter is used. A string specified by this parameter, and Spring's contextloaderlistener is responsible for decomposing the string into multiple configuration files, comma ",", Space "", and semicolon "; Can be used as a separator for a string. If neither the Applicationcontext.xml file nor the Contextconfiglocation parameter is used to determine the configuration file, or the configuration file Contextconfiglocation determined does not exist. Will cause spring to fail to load the configuration file or create the ApplicationContext instance correctly

The same effect can be achieved by configuring a servlet that spring sets for loading.

  Implemented with the Load-on-startup Servlet.
  Spring provides a special class of Servllet: contextloaderservlet. The servlet will automatically find the ApplicationContext of the web-in Kusakabe when it is started. XML file. Of course, in order for the Contextloaderservlet to start with the application startup, it is appropriate to configure this servlet to Load-on-startup servleto load-on-startup value. Because you want to ensure that ApplicationContext is created first. If there is only one configuration file and the file name is ApplicationContext. XML, add the following code to the Web. xml file:

<servlet>   <servlet-name>context</servlet-arne> <servlet-class> org.springframework.web.context.contextloaderservlet</servlet-class>   <load-on-startup >l</load-o Mouth-startup></servlet>

The Web. xml file with multiple configuration files is as follows:

<web-app>< ' Determine multiple profiles one ><context-param><!--parameter named Contextconfiglocation--><param-name>contextConfigLocation</param-name><!--multiple profiles with, separating one     )<param-value>/web-nf/daocontext.xml,/web-worker nf/Applicationcontext.xml</param-value> </context-param><! a load-on-startup Servlet to create a applicat Oncontext instance<servlet> <servlet-narne>context</servlet-narne> <servlet-class>Org.springframework.web.context.ContextLoaderServlet</servlet-class><!a little bit more appropriate, will first load a    <load-on-startup>l</load-on-startup> </servlet></web-app>

Second, use the match character

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/web-inf/ Applicationcontext*.xml</param-value></context-param>

Hibernate, for example, puts hibernate-related configuration in the Applicationcontext-hibernate.xml file, while some global-related information is placed in Applicationcontext.xml, Other configurations are similar. This allows you to load, without having to write with a space or comma separated!

Third, if you use struts to load multiple spring configuration files

The following configuration is actually a contextconfiglocation variable. Struts-config.xml, add this.

<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn" > <set-property property= " Contextconfiglocation "value="/web-inf/applicationcontext.xml,/web-inf/action-servlet.xml,,,,,,, "/>

Iv. if the non-Java EE application loads

New Classpathxmlapplicationcontext (new string[]{"Bean1.xml", "Bean2.xml"new   New  Xmlbeandefinitionreader (reg); Reader.loadbeandefinitions (new Classpathresource (" Bean1.xml ")); Reader.loadbeandefinitions (new classpathresource (" bean2.xml "= ( Beanfactory) reg;

(ext.) The role of Contextconfiglocation in spring in Web. xml

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.