標籤:style blog io color os ar java for sp
這幾天看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 those12 * components that use a separate component to perform configuration and13 * need to signal when configuration should be performed - usually after14 * {@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 LifecycleListener42 是所謂的接收者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, IOException76 --------------》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
tomcat載入web.xml