SpringMVC项目中web.xml中的节点加载顺序问题,之前以为web.xml中就是一些配置信息,和节点的顺序没有关系,后来才发现初始化时的加载顺序是和节点的顺序相关的。
完整的web.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring-Context loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring/*.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>authenticationFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>authenticationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>authorizationFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>authorizationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>com.auth.spring.listener.SpringLoaderListener</listener-class> </listener> <listener> <listener-class>com.abc.mobile.web.MobileLoader</listener-class> </listener> <listener> <listener-class> com.auth.listener.HttpSessionManager </listener-class> </listener> <!-- 防止spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>com.abc.conference.context.RpcManager</listener-class> </listener> <servlet> <servlet-name>Action</servlet-name> <servlet-class>com.abc.conference.apis.Action</servlet-class> </servlet> <servlet> <servlet-name>t1</servlet-name> <servlet-class>com.abc.base.web.init.BaseDataServlet</servlet-class> <!-- 如果需要自动加载,加下面一句 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Action</servlet-name> <url-pattern>/public/Action</url-pattern> </servlet-mapping> <servlet> <servlet-name>middleware</servlet-name> <servlet-class>com.abc.mobile.web.ActionProxy</servlet-class> </servlet> <servlet-mapping> <servlet-name>middleware</servlet-name> <url-pattern>/public/middleware</url-pattern> </servlet-mapping> <!-- spring mvc servlet --> <!-- springmvc 配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>ServletLoader</display-name> <servlet-name>ServletLoader</servlet-name> <servlet-class>com.abc.conference.context.ServletLoader</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <display-name>middlewareInit</display-name> <servlet-name>middlewareInit</servlet-name> <servlet-class>com.abc.middleware.init.MiddlewareServletLoader</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- 配置session超时时间,单位分钟 --> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
因为在Action中用到了一些静态变量,而这个静态变量是在BaseDataServlet中初始化的,所以如果
<servlet> <servlet-name>t1</servlet-name> <servlet-class>com.abc.base.web.init.BaseDataServlet</servlet-class> <!-- 如果需要自动加载,加下面一句 --> <load-on-startup>1</load-on-startup> </servlet>
配置节点,在Action:
<servlet-mapping> <servlet-name>Action</servlet-name> <url-pattern>/public/Action</url-pattern> </servlet-mapping>
节点之后的话,那调用静态变量的时候,静态变量的值就是Null,因为这时还没有初始化呢。
时间: 2024-10-14 10:21:39