Web. xml and web. xml configuration details
The web. xml deployment descriptor file is the configuration file for the web application, located in the WEB-INF directory. The Servlet container specifies the specific Servlet to process the specific request of the client according to the configuration in web. xml.
<Util>Tag
To use the <util> label, you must add the util namespace to the XML root element ):
<web-app xmlns:util="http://http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
Use tags such as <util: list>, <util: map>, <util: set>, and <util: properties> to replace ListFactoryBean, MapFactoryBean, SetFactoryBean, and PropertiesFactoryBean.
The <util: properties> label can also specify the location of its properties file through the location attribute.
<Util: properties id = "jdbcProps" location = "classpath: db. properties"/>
Use spring expressions in xml to obtain the values in the utlil element:
<Bean id = 'datasource 'class = 'xxx'>
<Property name = 'url' value = '# {jdbcProps. url}'> </property>
</Bean>
Use the @ Value annotation to obtain the Value in util before the setter method.
@ Value (# {jdbcProps. url })
Private String url;
<Context: property-placeholderLocation = "classpath *: conf/conf *. properties "/>: PropertyPlaceholderConfigurer implements the BeanFactoryPostProcessor interface. It can manage the attribute values in <bean/> externally and provide individual attribute files to manage relevant properties. Load properties to the spring context. Then, you can use $ {xx. yy} to access related properties when defining beans.
Classpath *: To load Multiple matching files at the same time, and classpath: only the first matching file can be loaded.
In Spring, classpath generally refers to the src directory.
<Context-param>Element
Declares context initialization parameters for the entire web application. This element can only appear in elements related to documents (icon, display-name, or description) and before the filter, filter-mapping, listener, or servlet elements.
TomcatContainer initialization process:
1. When a Web project is started, the container (Tomcat) reads the <listener> and <contex-param> nodes in the web. xml configuration file.
2. The Container creates a ServletContext, which is used by the entire web application.
3. The Container sends the value of the <context-param> element to ServletContext.
4. Create an instance in the <listener> </listener> container.
5. the listener class has a contextInitialized (ServletContextEvent event) initialization method. In this method, you can use the event. getServletContext (). getInitParameter ("contextConfigLocation") to get the value set by context-param. In this class, you must also have a contextDestroyed (ServletContextEvent event) destruction method to release resources before closing the application, such as closing the database connection.
6. After obtaining the value of context-param, you can perform some operations. The Web project has not been fully started yet.
7. The container loading process for web. xml is context-param-> listener-> fileter-> servlet.
<Context-param>And<Init-param>Differences
(1) <context-param> parameters in the entire application range are stored in ServletContext:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value></context-param>
Servlet:
String paramValue = getServletContext (). getInitParameter ("contextConfigLocation ")
(2) <init-param> is a parameter in the servlet range. It can only be obtained on the corresponding servlet or jsp page:
<servlet> <servlet-name>MainServlet</servlet-name> <servlet-class>com.wes.controller.MainServlet</servlet-class> <init-param> <param-name>param</param-name> <param-value>avalible in servlet init()</param-value> </init-param></servlet>
Servlet:
String paramValue = this. getInitParameter ("param ");
<Listener>Element
<listener> <listener-class>org.springframework.context.ContextLoaderListener</listener-class></listener>
Purpose:
Listener defined in the Servlet specification, used to listen to events generated by the Servlet container and handle the events accordingly.
The above ContextLoaderListener is an implementation class of ServletContextListener provided by Spring,It willFind WEB-INF at container InitializationApplicationContext. xmlFile (No contextConfigLocation),CreateSpringContainer. Spring containers manage bean objects according to their configuration files (bean is in the singleon scope by default, and applicationContext will be instantiated at startup ).
<Load-on-startup>Element
<! -Create the springmvc front-end controller during tomcat container initialization -->
<servlet> <servlet-name>taotao-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>taotao-manager</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
It is used to mark whether the tomcat container loads the servlet at startup (instantiate and call its init () method ). The value must be an integer, indicating the order in which the servlet should be loaded. The smaller the positive value, the higher the servlet priority, the more loaded the application is at startup. When the values are the same, the container selects the order for loading. If the value is smaller than 0 or is not specified, the container is loaded only when the servlet is requested. When the value is 0 or greater than 0, the container loads and initializes the servlet when the application starts.