SpringBoot 항목 (여섯) ----- 클래스 메소드를 실행 무엇을 시작 (다음)했던

이 문서는 다음과 같은 새로운 SpringApplication 말을했다, SpringApplication 생성자 짓을했다 (). 실행 방법

: 플로우 차트와 소스
그림 삽입 설명 여기
보기 ConfigurableApplicationContext 실행 코드 :

    public ConfigurableApplicationContext run(String... args) {
		// 创建计时器
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
		
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
		// 设置属性
        this.configureHeadlessProperty();
		
		// spring中监听器的使用,这些对象想创建,就得知道监听器的全路径
		// 会从spring.factory中读取
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        Collection exceptionReporters;
        try {
			// 初始化默认应用参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			// 根据监听器和默认的参数 来准备spring所需要的环境
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
			// 打印出banner
            Banner printedBanner = this.printBanner(environment);
			// 创建应用上下文
            context = this.createApplicationContext();
			// 异常报告
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
			// 准备应用上下文	
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
			// 刷新上下文
            this.refreshContext(context);
			// 刷新
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);
			// 执行callRunners, 支持自定义run方法
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

그런 다음 역할 == this.refreshContext (컨텍스트) ==이 방법 볼
의 흐름도 개선
그림 삽입 설명 여기
의 주요 방법 것은 (새로 고침) :

    public void refresh() throws BeansException, IllegalStateException {
        synchronized(this.startupShutdownMonitor) {
			// context设置 启动日期/当前状态/初始环境/验证
            this.prepareRefresh();
			// 创建一个bean工程,
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            this.prepareBeanFactory(beanFactory);

            try {
				// 注册scope 
                this.postProcessBeanFactory(beanFactory);
				// 调用所有bean工厂,对bean进行处理
                this.invokeBeanFactoryPostProcessors(beanFactory);
				// 拦截bean,
                this.registerBeanPostProcessors(beanFactory);
				// 国际化操作
                this.initMessageSource();
				// 广播事件
                this.initApplicationEventMulticaster();
				// 特殊bean 比如tomcat
                this.onRefresh();
				// 注册监听器
                this.registerListeners();
                this.finishBeanFactoryInitialization(beanFactory);
                this.finishRefresh();
            } catch (BeansException var9) {
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
                }

                this.destroyBeans();
                this.cancelRefresh(var9);
                throw var9;
            } finally {
                this.resetCommonCaches();
            }

        }
    }

그런 다음 차트 흐름, 후속 계속 :
그림 삽입 설명 여기

   public WebServer getWebServer(ServletContextInitializer... initializers) {
        if (this.disableMBeanRegistry) {
            Registry.disableRegistry();
        }

        Tomcat tomcat = new Tomcat();
        File baseDir = this.baseDirectory != null ? this.baseDirectory : this.createTempDir("tomcat");
        tomcat.setBaseDir(baseDir.getAbsolutePath());
        Connector connector = new Connector(this.protocol);
        connector.setThrowOnFailure(true);
        tomcat.getService().addConnector(connector);
        this.customizeConnector(connector);
        tomcat.setConnector(connector);
        tomcat.getHost().setAutoDeploy(false);
        this.configureEngine(tomcat.getEngine());
        Iterator var5 = this.additionalTomcatConnectors.iterator();

        while(var5.hasNext()) {
            Connector additionalConnector = (Connector)var5.next();
            tomcat.getService().addConnector(additionalConnector);
        }

        this.prepareContext(tomcat.getHost(), initializers);
        return this.getTomcatWebServer(tomcat);
    }

세계에서 사람들이 10 종류가 있습니다, 하나의 바이너리 이해하는 것입니다, 하나는 바이너리 이해하지 않습니다.

당신의 메시지를 작성하지 않는 경우, 감사합니다 경우, 시청 해 주셔서 감사합니다.

게시 71 개 원래 기사 · 원의 찬양 (54) · 전망 420 000 +

추천

출처blog.csdn.net/weixin_43326401/article/details/104077359