<context-param> <param-name>contextConfigLocation</param-name> <param-value> Contextconfiglocationvalue></param-value>
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener</listener-class></ Listener>
Role: This element is used to declare context initialization parameters within the scope of the application (the entire Web project)-typically a generic configuration bean throughout the project (typically used to load a bean other than the Web layer (such as dao, service, etc.) to facilitate integration with any other web framework).
Param-name sets the parameter name of the Context. Must be a unique name
Param-value sets the value of the parameter name, with multiple parameters separated by Commas.
- When you start a Web project, the container (such as Tomcat) reads the two nodes <listener> and <contex-param> in the. XML configuration File.
- The container then creates a ServletContext (context) that can be used by the entire Web project within the scope of the Application.
- The container then converts the read to <context-param> into a key-value pair and gives it to Servletcontext.
- The container creates a class instance in <listener></listener>, that is, the listener is created (note: the class defined by listener can be a custom class but must inherit servletcontextlistener).
- There is a contextinitialized (servletcontextevent Event) initialization method in the listener class, which can be passed Event.getservletcontext () in this method. Getinitparameter ("contextconfiglocation") to get the value Context-param set. There must also be a contextdestroyed (servletcontextevent Event) destruction method in this class. to release resources before closing the app, such as closing the database Connection.
- After you get the value of this context-param, you can do something about it. note that this time your Web project is not fully started yet. this action will be earlier than all Servlets.
It can be seen from:
contextloaderlistener initialized context-loaded beans are shared for the entire application, regardless of what presentation layer technology is used, generally such as DAO layer, service layer bean;
the dispatcherservlet initialized context-loaded Bean is a bean that is only valid for spring Web mvc, such as controller, handlermapping, handleradapter, and so On. The initialization context should only load web-related Components.
By the above initialization process, it is known that the container loading process for Web. XML is Context-param >> listener >> fileter >> servlet
Web. XML Configuration Detailed Context-param