How spring's configuration file is loaded in Web. xml

Source: Internet
Author: User

Web. Xml the way the spring configuration file is loaded depends mainly on the name of the profile and the location in which it is stored, and there are two main ways.

1. If the spring configuration file name is Applicationcontext.xml and is stored in the web-inf/directory, simply add the following code to the Web. xml

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

This listener automatically scans the APPLICATIONCONTEXT.XRNL file under web-inf/, which is mostly used in cases where there is only one configuration file.
You can also use this special servlet of Spring Contextloaderservlet, which implements the following code:

<!--spring3.0 is no longer supported in this way, so it is recommended to use the
<servlet> <servlet-name>context</servlet-name> <servlet-class> Org.springframework.web.context.contextloaderservlet</servlet-name>
<load-on-startup>1 (smaller numbers will do) </load-on-startup>
</servlet>

Both of the above are possible, but there are some differences.

2. If the name of the spring configuration file is a custom other name, such as Applicationcontext-test.xml, or is stored in the web-inf/directory, You also need to configure the Contextconfiglocation parameter, which is a string that the listener or servlet will customize to parse the string into multiple files by a specific character (such as a space, comma, semicolon) . You need to add the following code:

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/web-inf/ application-testa.xml,/web-inf/application-testb.xml,/web-inf/application-testb.xml</param-value></ Context-param>

If this is a problem, you can use wildcards to abbreviate the

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

Then add the above listener or servlet code to it.

3. 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 to create the ApplicationContext instance correctly.

Reprint: http://www.cnblogs.com/zjhs/archive/2012/10/26/2741228.html

How spring's configuration file is loaded 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.