Detailed introduction to Spring code instances using multiple xml configuration files

Source: Internet
Author: User
1. the contextConfigLocation parameter is defined in xml. spring will use this parameter to load. xml. without this parameter, spring loads the web-infapplicationContext.xml file by default. spring uses multiple xml configuration files. For more information, see.

1. the contextConfigLocation parameter is defined in xml. spring will use this parameter to load. xml. without this parameter, spring loads web-inf/applicationContext by default. xml file.

 
  
   contextConfigLocation
  
  
   classpath*:conf/spring/applicationContext_core*.xml,classpath*:conf/spring/applicationContext_dict*.xml,classpath*:conf/spring/applicationContext_hibernate.xml,classpath*:conf/spring/applicationContext_staff*.xml,classpath*:conf/spring/applicationContext_security.xmlclasspath*:conf/spring/applicationContext_modules*.xmlclasspath*:conf/spring/applicationContext_cti*.xmlclasspath*:conf/spring/applicationContext_apm*.xml
  
 

The contextConfigLocation parameter defines the Spring configuration file to be loaded. The principles are described as follows:

1. use ServletContextListener.
Spring provides an implementation class ContextLoaderListener for ServletContextListener, which can be used

Used for listener, which automatically locates the applicationContext. xrnl file under the WEB-INF/at creation. Because
If there is only one configuration file named applicationContext. xml

Add the following code to the file:

 
  
   org.springframework.web.context.ContextLoaderListener
  
 

If multiple configuration files need to be loaded, consider using File name. Because the ContextLoaderListener will find the parameter named contextConfigLocation when loading.

Therefore, the parameter name should be contextConfigLocation when context-param is configured.
The web. xml file with multiple configuration files is as follows:

<1 -- XML file header 2> 〉
 <1 -- DTD and other information of the web. xm1 file 1> 〉
 
  
  
   
<1 -- the parameter name is contextConfigLocation... > 〉
   
    
ContextConfigLocation
   
   /WEB-machine NF/daoContext. xml./WEB-INF/applicationContext. xml
  
  
  
   
    
Org. spr ngframework. web. context. ContextLoaderListener
   
  
 

If no configuration file is specified for contextConfigLocation, Spring automatically searches for the application
Context. xrnl configuration file. If contextConfigLocation exists, the configuration file determined by this parameter is used.

A string specified by this parameter. Spring's ContextLoaderListener is responsible for breaking this string into multiple
The configuration file, comma ",", Space "", and semicolon ";" can be used as character strings.

If neither the applicationContext. xrnl file nor the contextConfigLocation parameter is used
Configuration file, or the configuration file specified by contextConfigLocation does not exist. Will cause Spring to fail

The configuration file is loaded or the ApplicationContext instance cannot be created normally.
Configuring a spring servlet for loading can achieve the same effect.

Load-on-startup Servlet is used.
Spring provides a special Servllet class: ContextLoaderServlet. When the Servlet is started

Automatically searches for the applicationContext. xml file on the WEB-IN day.
Of course, to enable the ContextLoaderServlet to start with the application, you should configure this Servlet

The value of load-on-startup's Servleto load-on-startup is a little smaller, because you must ensure that the Application
Context is created first. If there is only one configuration file named applicationContext. xml

Add the following code to the web. xml file:

 
  
   context
  
  
   org.springframework.web.context.ContextLoaderServlet
  
  
   l
  
 

. The web with multiple configuration files and the nl file is as follows:

 
 
 
 
  
<'One determine multiple configuration files 1'>
  
   
   
    
ContextConfigLocation
   
   
  
 

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.