tomcat加载web.xml
这几天看tomcat的源码,疑问很多,比如之一“ tomcat 怎么加载 web.xml”,下面是跟踪的过程,其中事件监听器有一个观察者模式,比较好。记录下来以供参考
1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>tomcat load web.xml>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2 3 4 5 tomcat加载web.xml中用户的servlet到context中去,同时实例化该servlet到/WEB-INF/classes/中 6 过程大致如下 7 8 由事件接收器 接收 到CONFIGURE_START_EVENT的消息后,才会触发处理加载web.xml的动作, 9 10 /** 11 * The LifecycleEvent type for the "configure_start" event. Used by those 12 * components that use a separate component to perform configuration and 13 * need to signal when configuration should be performed - usually after 14 * {@link #BEFORE_START_EVENT} and before {@link #START_EVENT}. 15 */ 16 public static final String CONFIGURE_START_EVENT = "configure_start"; 17 18 那么又是谁来把该消息注册到监听器中的呢?如下,从启动过程中进行查找: 19 20 21 org.apache.catalina.startup.Bootstrap.start() 22 -->org.apache.catalina.startup.Catalina.start() 23 ----》load() 24 ------》 25 getServer().init(); 26 Server==="org.apache.catalina.core.StandardServer" 27 -----》StandardServer.initInternal() 28 StandardServer.startInternal() 29 ---》 30 void java.org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(String type, Object data) 31 32 fireLifecycleEvent(CONFIGURE_START_EVENT, null); 33 ---》 34 LifecycleSupport fireLifecycleEvent(CONFIGURE_START_EVENT, null); 35 36 由LifecycleSupport 来广播给所有的接收者, 37 接收者根据需要来判断是否需要处理【此处用到观察者模式 】 38 39 40 41 而public class ContextConfig implements LifecycleListener 42 是所谓的接收者 43 其重写的 lifecycleEvent(LifecycleEvent event)方法中 44 45 if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) { 46 47 configureStart();该方法是进行加载的具体方法 48 49 50 protected synchronized void configureStart() 51 -----------------》webconfig() 52 ::::: 53 54 webconfig()中 55 { 56 String resource = 57 "/WEB-INF/classes/" + binding.getName(); 58 try { 59 URL url = sContext.getResource(resource); 60 processAnnotationsUrl(url, webXml, 61 webXml.isMetadataComplete()); 62 } catch (MalformedURLException e) { 63 log.error(sm.getString( 64 "contextConfig.webinfClassesUrl", 65 resource), e); 66 } 67 68 } 69 70 ---------------》 71 void java.org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(URL url, WebXml fragment, boolean handlesTypesOnly) 72 ----------》 73 void java.org.apache.catalina.startup.ContextConfig.processAnnotationsJndi(URL url, WebXml fragment, boolean handlesTypesOnly) 74 ------------》 75 void java.org.apache.catalina.startup.ContextConfig.processAnnotationsStream(InputStream is, WebXml fragment, boolean handlesTypesOnly) throws ClassFormatException, IOException 76 --------------》 77 void java.org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(String className, AnnotationEntry ae, WebXml fragment) 78 79 ---------》 80 fragment.addServlet(servletDef); 81 fragment.addServletMapping(urlPattern, servletName); 82 83 84 ------->webXml.configureContext(context); 85 86 public void configureContext(Context context) { 87 Wrapper wrapper = context.createWrapper(); 88 context.addChild(wrapper); 89 context.addServletMapping(entry.getKey(), entry.getValue()); 90 91 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>tomcat load web.xml>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 92 93