美文网首页Spring
Spring Boot启动-构造ApplicationConte

Spring Boot启动-构造ApplicationConte

作者: 王勇1024 | 来源:发表于2019-02-24 15:56 被阅读1次

    创建ApplicationContext实现代码如下:

    1. 根据Web应用类型,获取对应的ApplicationContext子类
    2. 实例化ApplicationContext子类
    // 常量定义
    public static final String DEFAULT_WEB_CONTEXT_CLASS = "org.springframework.boot."
                + "web.servlet.context.AnnotationConfigServletWebServerApplicationContext";
    public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
                + "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";
    public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context."
                + "annotation.AnnotationConfigApplicationContext";
    
        protected ConfigurableApplicationContext createApplicationContext() {
            // 1. 根据Web应用类型,获取对应的ApplicationContext子类
            Class<?> contextClass = this.applicationContextClass;
            if (contextClass == null) {
                try {
                    switch (this.webApplicationType) {
                    case SERVLET:
                        contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);
                        break;
                    case REACTIVE:
                        contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                        break;
                    default:
                        contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
                    }
                }
                catch (ClassNotFoundException ex) {
                    throw new IllegalStateException(
                            "Unable create a default ApplicationContext, "
                                    + "please specify an ApplicationContextClass",
                            ex);
                }
            }
            // 2. 实例化子类
            return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
        }
    

    BeanFactory也是在此过程中被创建

        public GenericApplicationContext() {
            this.beanFactory = new DefaultListableBeanFactory();
        }
    

    相关文章

      网友评论

        本文标题:Spring Boot启动-构造ApplicationConte

        本文链接:https://www.haomeiwen.com/subject/iocbyqtx.html