Web. XML is loaded by the file, summary of each node loading sequence
Blog Category:
Webxmlspringservletbean
Today, 2010-3-11 Day, to go to work no matter, want to put the Web. XML Project description file loading process to do a summary paste here, in case of forgetting:
Web. XML loading process (steps):
1. When you start a Web project, the container (for example, Tomcat) reads its configuration file Web. Read TWO nodes:
<listener></listener> and <context-param></context-param>
2. Immediately after the container creates a servletcontext (context), all parts of the Web project will share this context.
3. The container converts <context-param></context-param> into a key-value pair and gives it to ServletContext.
4. The container creates a class instance in <listener></listener>, that is, the listener is created.
5. There is a contextinitialized (Servletcontextevent args) initialization method in the listener, which is obtained in this method:
Java code
- ServletContext = Servletcontextevent.getservletcontext ();
- The value of Context-param = Servletcontext.getinitparameter ("Key of Context-param");
6. 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. In other words, this time, your actions on key values in <context-param> will be executed before your Web project is fully booted.
7. For example, you might want to open the database before the project starts. You can then set up the database connection in <context-param> and initialize the database connection in the Listener class.
8. This listener is a class of its own, in addition to the initialization method, it also has the method of destruction. Frees the resource before closing the app. For example, the database connection is closed.
Web. XML node Load order:
To be sure, the order in which nodes are loaded is independent of their order in the Web. xml file. That is, the filter is not loaded before the filter is written in front of the listener. The final conclusion is: Listener, filter, servlet
There is also a configuration node: Context-param, which is used to provide ServletContext with key-value pairs, which are application context information. Our listener, filter, and so on are initialized with the information in these contexts, so should the Context-param configuration section be written before the Listener configuration section? In fact, the Context-param configuration section can be written anywhere, so the real load order is:
Context-param, listener, filter-a servlet
For a class of configuration sections, it is related to the order in which they appear. In the case of filter, it is possible to define multiple filter in Web. XML, a configuration section related to filter is filter-mapping, and it is important to note that for filter and filter-mappin with the same filter-name In the case of the G configuration section, the filter-mapping must appear after the filter, or the corresponding filter-name is undefined when parsing to filter-mapping. When each filter is initialized at the start of the Web container, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches multiple filter-mapping, the filter interception resource is invoked in the order in which the Filter-mapping configuration section appears in sequence D The Ofilter () method.
The servlet is similar to filter and is not mentioned here.
"Load Spring"
For example, the filter needs to use the bean, but the load order is: Load the filter after loading spring, the filter in the initialization operation of the bean is null;
So, if you want to use a bean in a filter, you can change the load of spring to listener:
XML code
- <listener>
- <listener-class>
- Org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
Final conclusion:
The order in which Web. XML is loaded is: [Context-param---listener, filter--, servlet--spring], whereas the Order of actual program calls between nodes of the same type is based on the corresponding Mappin The order of the G is called.
Web. XML is loaded by the file, summary of each node loading sequence