1, define the Contextconfiglocation parameter in Web. Xml. Spring uses this parameter to load the. All comma-delimited and all-in. If this parameter is not available, spring defaults to load web-inf/ Applicationcontext.xml file.
The contextconfiglocation parameter defines the Spring configuration file to be loaded. The principle is described as follows:
1. Implement with Servletcontextlistener.
Spring provides an implementation class Contextloaderlistener for Servletcontextlistener, which can be used as a
Used for listener, it will automatically find the Applicationcontext.xrnl file under web-inf/when it is created. Because
This, if there is only one configuration file, and the file name is Applicationcontext.xml, only the Web. xml
Add the following code to the 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-para element to determine the configuration file's
Filename. Because Contextloaderlistener is loaded, a parameter named Contextconfiglocation is found.
Therefore, the parameter name should be contextconfiglocation when configuring Context-param.
The Web. xml file with multiple configuration files is as follows:
<1--XML File Header II
<?xml version= "L.O" encoding= "Gong 80-8859-1"?>
< WEB.XM1 file DTD and other information
<! DOCTYPE Web-app
Public "-//sun Microsystems. Work Port C.//DTD Web application 2.3//en "
' Http://java.sun.com/dtd/web-app_2_3.dtd ' >
<web-app>
<! a certain number of configuration files >
<context-param>
The <1--parameter is named contextconfiglocation...〉
<param-name>contextConfigLocation</param-name>
<! between more than one configuration file, separated by two
<param-value>/web-worker Nf/daocontext.xml./web-inf/application
Context.xml</param-value>
</context-param>
<!--using listener to create Applicat oncontext instances--
<listener>
<LISTENER-CLASS>ORG.SPR worker Ngframework.web.context.ContextLoader
Listener</listener-class>
</listener>
</web-app>
If the configuration file is not contextconfiglocation specified, Spring automatically finds application
Context. XRNL 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 breaking the string into multiple
Config file, comma ",", Space "" and semicolon ";" Can be used as a separator for a string.
If there is neither applicationcontext. XRNL file, nor do you use the Contextconfiglocation parameter
configuration file, or contextconfiglocation determined configuration file does not exist. Will cause spring to fail
Loading a configuration file or not creating a ApplicationContext instance as expected
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. When the servlet is started, it will
Automatically finds the applicationcontext of Web-in Kusakabe. XML file.
Of course, in order for the Contextloaderservlet to start with the application, you should configure this servlet to
The value of the Load-on-startup Servleto Load-on-startup is a little bit more appropriate, because it is guaranteed to application
Context first created. If there is only one configuration file and the file name is ApplicationContext. XML, then in
Add the following code to the Web. xml file:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.contextloaderservlet</
Servlet-class>
<load-on-startup>l</load-on-startup>
</servlet>
。 The web with multiple profiles and the NL file is as follows:
<!--file header for an XML file--
<?xml version= "1.0" encoding= "Gong so-8859-1"?>
<! --Web. xml file dtd and other information →
<! DOCTYPE Web-app
Public "-//sun Microsystems, Port c.//dtd Web application 2.3//en"
' Http://java.sun.com/dtd/web-app_2_3.dtd ' >
<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>
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.