Go The Contextloaderlistener in spring uses

Source: Internet
Author: User

When using spring in a recent project, there is no beanfactory in the Applicationcontext.xml configuration file, and to refer to a spring container-managed bean directly in the class file in the business layer can be

1. config listener Contextloaderlistener in Web. xml

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

The role of Contextloaderlistener is to automatically assemble ApplicationContext configuration information when the Web container is started. Because it implements the Servletcontextlistener interface, when the Web. XML configures this listener, when the container is started, it executes the method it implements by default.

2. Deploying ApplicationContext XML files

How to deploy the ApplicationContext XML file, if no parameter configuration information is written in Web. XML, the default path is "/web-inf/ Applicationcontext.xml, the name of the XML file created under the Web-inf directory must be applicationcontext.xml. If you want to customize the file name, you can add contextconfiglocation to the context parameter in Web. XML:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/web-inf/classes/applicationcontext-*.xml
</param-value>
</context-param>

In <param-value> </param-value> Specify the appropriate XML file name, if there are multiple XML files, can be written together and one "," number separated. The applicationcontext-*.xml above uses a wildcard character, For example, in this directory there are applicationcontext-ibatis-base.xml,applicationcontext-action.xml,applicationcontext-ibatis-dao.xml and other files, will be loaded together.

This shows that the Applicationcontext.xml file location can have two default implementations:

First: Put it directly under/web-inf, in the Web. XML, declare a listener,

The second kind: put it under Classpath, but add <context-param> to the Web. XML, and use it to indicate the location of your applicationcontext.xml to be loaded by the container. According to Struts2, the official files of spring are written as follows:
<!--Context Configuration locations for Spring XML files----
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>

3. Call Place

The Contextloader class is associated with Contextloaderlistener, so the entire load configuration process is done by Contextloader. Take a look at its API description

The first paragraph shows that contextloader can be generated by Contextloaderlistener and Contextloaderservlet. If you look at the Contextloaderservlet API, you can see that it also associates the Contextloader class and it implements the HttpServlet. This interface

In the second paragraph, Contextloader creates a class Xmlwebapplicationcontext that implements an interface that is webapplicationcontext-> Configurablewebapplicationcontext->applicationcontext->

Beanfactory so that all the beans in spring are created by this class

Iuploaddatafilemanager UploadManager = (Iuploaddatafilemanager) Contextloaderlistener.getcurrentwebapplicationcontext (). Getbean ("UploadManager");

Go The Contextloaderlistener in spring uses

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.