Spring configuration in web. xml: springweb. xml

Source: Internet
Author: User

Spring configuration in web. xml: springweb. xml

Spring configuration in web. xml

Preface

In actual projects, the spring configuration file applicationcontext. xml is automatically loaded to the container through the loading mechanism provided by spring. In a web project, the configuration file is loaded into the web container for parsing. Currently, spring provides two loaders for loading web containers:One is ContextLoaderListener and the other is ContextLoaderServlet.. The two functions are identical, but the former is implemented based on the Listener interface introduced in Servlet2.3, and the latter is implemented based on the Servlet interface, the following are the two loaders on the web. configuration application in xml:

ContextLoaderListener

<listener> <listener-class>org.springframework.context.ContextLoaderListener</listener-class> </listener> 

ContextLoaderServlet

<servlet>  <servlet-name>context</servlet-name> <servlet-class>org.springframework.context.ContextLoaderServlet</servlet-class>  <load-on-startup>1</load-on-startup> </servlet> 

With the preceding configuration, the web Container automatically loads applicationcontext. xml for initialization.

To specify the location of the configuration file, you can use context-param to specify the location:

<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/myApplicationContext.xml</param-value> </context-param> 

Then, you can use the WebApplicationContextUtils. getWebApplicationContext method to obtain the reference of applicationcontext in the web application.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.