tomcat源码之处理web.xml中<load-on-startup>标签
2017-06-20 14:17
429 查看
先上源码(tomcat7.0):
代码很清晰简洁,很容易看懂。
总结:如果web.xml中的servlet没有配置
/** * Load and initialize all servlets marked "load on startup" in the * web application deployment descriptor. * * @param children Array of wrappers for all currently defined * servlets (including those not declared load on startup) */ /** * StandardContext类,代表webapps目录下的一个web应用 * 方法会被startInternal调用(),也就是应用在启动的时候 * @param children * @return */ public boolean loadOnStartup(Container children[]) { // Collect "load on startup" servlets that need to be initialized //用TreeMap来排序 默认为natural order,也就是按ascii码 TreeMap<Integer, ArrayList<Wrapper>> map = new TreeMap<Integer, ArrayList<Wrapper>>(); for (int i = 0; i < children.length; i++) { Wrapper wrapper = (Wrapper) children[i]; //每个Wrapper对应一个Servlet,负责创建、初始化、销毁servlet,并为其提供ServletConfig int loadOnStartup = wrapper.getLoadOnStartup(); if (loadOnStartup < 0) //如果loadOnStartup的配置参数小于0,则不添加到TreeMap中 continue; Integer key = Integer.valueOf(loadOnStartup); ArrayList<Wrapper> list = map.get(key); if (list == null) { list = new ArrayList<Wrapper>(); map.put(key, list); } list.add(wrapper); } // Load the collected "load on startup" servlets //按loadOnStartup 的natural order即升序,依次对servlet进行初始化 for (ArrayList<Wrapper> list : map.values()) { for (Wrapper wrapper : list) { try { //此方法包含对servlet的实例化和init方法的调用 wrapper.load(); } catch (ServletException e) { getLogger().error(sm.getString("standardContext.loadOnStartup.loadException", getName(), wrapper.getName()), StandardWrapper.getRootCause(e)); // NOTE: load errors (including a servlet that throws // UnavailableException from the init() method) are NOT // fatal to application startup // unless failCtxIfServletStartFails="true" is specified if (getComputedFailCtxIfServletStartFails()) { return false; } } } } return true; }
代码很清晰简洁,很容易看懂。
总结:如果web.xml中的servlet没有配置
<load-on-startup>标签或者其值小于0,那么在容器启动时不会对其初始化;否则按照数字升序排列串行对其初始化(这也证实了
正数的值越小,启动该servlet的优先级越高这句话)。
相关文章推荐
- web.xml中Servlet配置<load-on-startup>的作用
- web.xml配置中<load-on-startup>0</load-on-startup>的一个问题
- web.xml中<load-on-startup></load-on-startup>中的作用
- web.xml参数<load-on-startup>1</load-on-startup>
- web.xml中 <load-on-startup>笔记
- web.xml中配置<load-on-startup>的作用
- <load-on-startup> 标签小议
- SpringMVC: web.xml中声明DispatcherServlet时一定要添加load-on-startup标签
- web.xml中servlet load-on-startup标签的含义
- javaweb中web.xml中servlet配置项<load-on-startup>配置项含义
- tomcat里web.xml中load-on-startup参数含义
- tomcat里web.xml中load-on-startup参数含义
- web.xml中load-on-startup标签的含义 .
- tomcat里web.xml中load-on-startup参数含义
- web.xml中load-on-startup标签的含义 .
- SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
- web.xml中load-on-startup标签的含义
- java的web配置文件的“<load-on-startup>的说明[转]
- web.xml中load-on-startup标签的含义
- web.xml中load-on-startup标签的含义