您当前的位置: 首页 >  spring

java持续实践

暂无认证

  • 1浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot 2.x 框架整体启动流程概览

java持续实践 发布时间:2021-06-13 11:55:04 ,浏览量:1

文章目录
      • 启动流程
      • 完整的启动流程图

启动流程

主要分为三步: 1.框架初始化 2. 框架启动 3. 自动化装配

  • 框架初始化: new SpringApplication(primarySources).run(args); 调用SpringApplication的构造方法 构造方法中, 执行如下的代码, 进行框架的初始化

    public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
    		// 1. 配置资源加载器
    		this.resourceLoader = resourceLoader;
    		Assert.notNull(primarySources, "PrimarySources must not be null");
    		// 2.  配置primarySources  
    		this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
    		// 3. 应用环境监测 
    		this.webApplicationType = WebApplicationType.deduceFromClasspath();
    		// 4. 配置系统初始化器 
    		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    		// 5. 配置应用监听器  
    		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    		// 6. 配置main 方法所在的类  
    		this.mainApplicationClass = deduceMainApplicationClass();
    	}
    
  • 框架启动: SpringApplication 初始化完成后, 调用run方法, 进行框架的启动. run方法如下

    	public ConfigurableApplicationContext run(String... args) {
    	  // 1. 计时器开始计时 
    		StopWatch stopWatch = new StopWatch();
    		stopWatch.start();
    		ConfigurableApplicationContext context = null;
    		Collection exceptionReporters = new ArrayList();
    		// 2. Headless 模式赋值 : 工作在一个没有显示器键盘的环境
    		configureHeadlessProperty();
    		// 3. 发送ApplicationStartingEvent  监听事件 
    		SpringApplicationRunListeners listeners = getRunListeners(args);
    		listeners.starting();
    		try {
    				// 4. 配置环境模块
    			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    	//  5. 发送 ApplicationEnvironmentPreparedEvent
    			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
    			configureIgnoreBeanInfo(environment);
    			// 6. 打印banner
    			Banner printedBanner = printBanner(environment);
    			// 7. 创建应用上下文对象 
    			context = createApplicationContext();
    			// 8. 初始化失败分析器 
    			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
    					new Class[] { ConfigurableApplicationContext.class }, context);
    					// 9. 关联SpringBoot 组件和应用上下文对象 
    					// 10. 在prepareContext 方法中发送ApplicationContextInitializedEvent 
    			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
    			//  11. 加载sources到context当中
    			 // 12. 发送ApplicationPreparedEvent 
    			 // 13. 刷新上下文
    			refreshContext(context);
    			afterRefresh(context, applicationArguments);
    			// 14. 计时器停止计时 . 
    			stopWatch.stop();
    			if (this.logStartupInfo) {
    				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
    			}
    			// 15. 发送ApplicationStartedEvent事件
    			listeners.started(context);
    			// 16. 调用框架启动扩展类
    			// 17. 发送ApplicationReadyEvent 事件
    			callRunners(context, applicationArguments);
    		}
    		catch (Throwable ex) {
    			handleRunFailure(context, ex, exceptionReporters, listeners);
    			throw new IllegalStateException(ex);
    		}
    
    		try {
    			listeners.running(context);
    		}
    		catch (Throwable ex) {
    			handleRunFailure(context, ex, exceptionReporters, null);
    			throw new IllegalStateException(ex);
    		}
    		return context;
    	}
    
  • 自动化装配:

    1. 收集配置文件中的配置工厂类
    2. 加载组件工厂
    3. 注册组件内定义的bean
完整的启动流程图

完整的启动流程图如下. 在线浏览图片 https://www.processon.com/view/link/60c580aa7d9c0879372476bd 在这里插入图片描述

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0419s