Learning spring from source code

       When Jetty starts, it starts the Server first, calls the dostart method of the parent class HandlerWrapper of jettyserver, starts the handler, and the handler is a HandlerWrapper, which contains 8 Handlers in total, and the start method of the Handler is called cyclically to start each Handler.
        Among them, XX is responsible for parsing the basic information of webapp, parsing web.xml, and saving the information to the entity. Then call doStart  of WebAppContext to  start, record and call the contextInitialized  method of all Listeners parsed and loaded in web.xml, and start the listener. 
       The analysis of this paper mainly includes: the analysis and startup of webapp, the processing of requests, the management of permissions, and the analysis of jsp.
(1) The parsing and startup of webapp includes:
  1.     Parsing and launching of webapp
  2.     Spring startup and Bean loading, etc.
(2) The processing of requests includes:
  1.     How jetty handles the request and forwards it to spring 
  2.     How spring handles requests
(3) The management of authority includes:
  1.    How jetty manages cookies and sessions
  2.    How spring security manages permissions
(4) Analysis of jsp
  1.    How to convert jsp to servlet
  2.    How each template engine works
 

1. Analysis and startup of webapp

 
 When the service starts, the server's dostart method is called. The dostart method calls the dostart method of the parent class HandlerWrapper. The dostart method starts the Handler. This Handler is a HandlerCollection that contains three elements and starts three Handlers in a loop.
  1.  0 = {ContextHandlerCollection@3593} 
  2.  1 = {DefaultHandler@3633} 
  3.  2 = {RequestLogHandler@3634} 
        The ContextHandlerCollection is the deployed app. There are as many JettyWebAppContext elements as there are wars under the webapp directory . The start method of JettyWebAppContext is called cyclically to start the deployment and analysis of the app.

1. Analysis of web.xml    

      The class hierarchy of JettyWebAppContext is as follows:
     JettyWebAppContext >  WebAppContext > ServletContextHandler > ContextHandler > ScopeHandler > HandlerWrapper > AbstractHandlerContainer > AbstractHandler >  AggregateLifeCycle >  AbstractLifeCycle 
 
protected void doStart() throws Exception {
        try {
            this._metadata.setAllowDuplicateFragmentNames(this.isAllowDuplicateFragmentNames());
            this.preConfigure();
            super.doStart ();
            this.postConfigure();
            if(this.isLogUrlOnStart()) {
                this.dumpUrl();
            }
        } catch (Exception var2) {
            LOG.warn("Failed startup of context " + this, var2);
            this._unavailableException = var2;
            this.setAvailable(false);
            if(this.isThrowUnavailableOnStartupException()) {
                throw var2;
            }
        }
    }
 
dostart方法的调用堆栈如下:
at org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitSessionConfig(StandardDescriptorProcessor.java:667)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.visit(IterativeDescriptorProcessor.java:85)
at org.eclipse.jetty.webapp.IterativeDescriptorProcessor.process(IterativeDescriptorProcessor.java:72)
at org.eclipse.jetty.webapp.MetaData.resolve(MetaData.java:366)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1240) ContextHandler的startContext方法
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:298) 
 
其中 _configurations 包含如下:
0 = {MavenWebInfConfiguration@3811}
1 = {WebXmlConfiguration@3812}
2 = {MetaInfConfiguration@3813}
3 = {FragmentConfiguration@3814}
4 = {EnvConfiguration@3675}
5 = {PlusConfiguration@3815}
6 = {MavenAnnotationConfiguration@3816}
7 = {JettyWebXmlConfiguration@3817}
 
MavenWebInfConfiguration主要处理webinf目录和一些临时目录等相关的配置。
WebXmlConfiguration负责定位和解析web.xml配置文件。
MetaInfConfiguration负责解析jar包。
 
org . eclipse . jetty . webapp . WebAppContext . startContext最后负责启动所有的Listener,如果使用了Spring框架,此时会启动Spring的ContextLoaderListener。
0 = {ELContextCleaner@5759}
1 = {IntrospectorCleaner@5760}
2 = {LogbackConfigListener@5761}
3 = {SystemLoaderListener@5762}
4 = {OnlineInfoListener@5763}
 
在WebAppContext的启动过程中启动了SessionHandler。
at org.eclipse.jetty.server.session.HashSessionManager.doStart(HashSessionManager.java:92)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
- locked <0x12c1> (a java.lang.Object)
at org.eclipse.jetty.server.session.SessionHandler.doStart(SessionHandler.java:123)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
- locked <0x1287> (a java.lang.Object)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:115)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:763)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
 
 

2、Spring的启动

 
第一步解析并把bean的定义加载到集合中,此时不会初始化bean,因为bean可能存在以来关系。
第二步分析bean的依赖关系.
第三步创建并加载bean
bean的解析和加载是一个递归的过程。
 
如果是XML的context配置,通过web.xml配置文件找到Spring的初始配置文件,并解析。包括外部文件、component-scan、bean的创建、资源的映射等的处理。
其中parseDefaultElement方法根据不同的类型进行不同的处理。

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326866239&siteId=291194637