Spring源码分析【3】-SpingWebInitializer的加载

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】

SpingWebInitializer的加载

Spring基于注解的配置代码:

    public class SpingWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[]{RootConfig.class};
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[]{WebConfig.class};
        }

        @Override
        protected String[] getServletMappings() {
            return new String[]{"/"};
        }
    }

整体调用UML图:

20191017100206\_1.png

SpringServletContainerInitializer.onStartup传入参数:

20191017100206\_2.png

类型判断必须是继承WebApplicationInitializer接口:

20191017100206\_3.png

isAssignableFrom 是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的超类或接口。
通常调用格式是
Class1.isAssignableFrom (Class2)
调用者和参数都是 java.lang.Class 类型。

而 instanceof 是用来判断一个对象实例是否是一个类或接口的或其子类子接口的实例。
格式是: oo instanceof TypeName
第一个参数是对象实例名,第二个参数是具体的类名或接口名

参考:Class.isAssignableFrom与 instanceof 区别

List initializers:

20191017100206\_4.png

onStartup()调用链:

AbstractDispatcherServletInitializer 有onStartup()

->AbstractAnnotationConfigDispatcherServletInitializer 无onStartup()

->SpingWebInitializer 无onStartup()

20191017100206\_5.png

调用父类的AbstractContextLoaderInitializer的onStartup()

20191017100206\_6.png

调用createRootApplicationContext()

20191017100206\_7.png

createRootApplicationContext()调用链:

AbstractAnnotationConfigDispatcherServletInitializer有createRootApplicationContext()

->AbstractDispatcherServletInitializer无createRootApplicationContext()

->AbstractContextLoaderInitializer调用createRootApplicationContext()

继承的调用链是逐级往下逐级往上

终于调用到getRootConfigClasses()开始第一阶段

20191017100206\_8.png

register就是往set集合增加类

    public void register(Class<?>... annotatedClasses) {
            Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
            this.annotatedClasses.addAll(Arrays.asList(annotatedClasses));
        }

20191017100206\_9.png

20191017100206\_10.png

回到registerContextLoaderListener

20191017100206\_11.png

ContextLoaderListener

20191017100206\_12.png

ContextLoader

20191017100206\_13.png

返回ContextLoaderListener

20191017100206\_14.png

调用tomcat的ApplicationContextFacade implements ServletContext 调用addListener

20191017100206\_15.png

调用addListener

20191017100206\_16.png

addApplicationLifecycleListener

20191017100206\_17.png

applicationLifecycleListenersObjects

20191017100206\_18.png

newListeners

20191017100206\_19.png

回到registerContextLoaderListener

20191017100206\_20.png

完成onStartup

20191017100206\_21.png

进入registerDispatcherServlet

20191017100206\_22.png

调用createServletApplicationContext

20191017100206\_23.png

开始第二阶段进入SpingWebInitializer的getServletConfigClasses

20191017100206\_24.png

register同第一阶段

20191017100206\_25.png

回到registerDispatcherServlet

20191017100206\_26.png

进入tomcat的ApplicationContextFacade addServlet

20191017100206\_27.png

addServlet

20191017100206\_28.png

addChild

20191017100206\_29.png

super.addChild

20191017100206\_30.png

addChildInternal

20191017100206\_31.png

children

20191017100206\_32.png

回到registerDispatcherServlet

20191017100206\_33.png

第三次进入SpingWebInitializer

20191017100206\_34.png

回到registerDispatcherServlet

20191017100206\_35.png

回到onStartup

20191017100206\_36.png

回到onStartup

20191017100206\_37.png

退出到Tomcat的StandardContext的startInternal()

20191017100206\_38.png

entry.getKey()

20191017100206\_39.png

initializers

20191017100206\_40.png


来源:[]()

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Spring源码分析【3】-SpingWebInitializer的加载

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏