zoukankan      html  css  js  c++  java
  • Spring Boot启动流程

    一、一行代码完成启动

    @SpringBootApplication
    public class Sb2Application {
    
    	public static void main(String[] args) {
    		SpringApplication.run(Sb2Application.class, args);
    	}
    
    }
    

      

    进入SpringApplication的run方法

    SpringApplication.run(Sb2Application.class, args);

    分为两步: 一步是初始化SpringApplication, new SpringApplication,第二步是调用SpringApplication的run方法

    第一步:初始化SpringApplication

     public SpringApplication(Class... primarySources) {
            this((ResourceLoader)null, primarySources);
        }
    
        public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
            this.sources = new LinkedHashSet();
            this.bannerMode = Mode.CONSOLE;
            this.logStartupInfo = true;
            this.addCommandLineProperties = true;
            this.addConversionService = true;
            this.headless = true;
            this.registerShutdownHook = true;
            this.additionalProfiles = new HashSet();
            this.isCustomEnvironment = false;
            this.lazyInitialization = false;
            this.resourceLoader = resourceLoader;
            Assert.notNull(primarySources, "PrimarySources must not be null");
            this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
            this.webApplicationType = WebApplicationType.deduceFromClasspath();
            this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
            this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
            this.mainApplicationClass = this.deduceMainApplicationClass();
        }
    

      

    第二步 调用SpringApplication的run方法

    /**
    	 * Run the Spring application, creating and refreshing a new
    	 * {@link ApplicationContext}.
    	 * @param args the application arguments (usually passed from a Java main method)
    	 * @return a running {@link ApplicationContext}
    	 */
    	public ConfigurableApplicationContext run(String... args) {
    		StopWatch stopWatch = new StopWatch();
    		stopWatch.start();
    		ConfigurableApplicationContext context = null;
    		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    		configureHeadlessProperty();
    		SpringApplicationRunListeners listeners = getRunListeners(args);
    		listeners.starting();
    		try {
    			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
    			configureIgnoreBeanInfo(environment);
    			Banner printedBanner = printBanner(environment);
    			context = createApplicationContext();
    			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
    					new Class[] { ConfigurableApplicationContext.class }, context);
    			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
    			refreshContext(context);
    			afterRefresh(context, applicationArguments);
    			stopWatch.stop();
    			if (this.logStartupInfo) {
    				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
    			}
    			listeners.started(context);
    			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、自动化装配

     

    1、框架初始化步骤

    框架初始化步骤:

    配置资源加载器

    配置primarySources

    应用环境检测

    配置系统初始化器

    配置应用监听器

    配置main方法所在的类

    2、框架启动流程

    计时器开始计时 -> Headless模式赋值 ->  发送ApplicationStartEvent

    -> 配置环境模块 -> 发送ApplicationEnvironmentPreparedEvent

    -> 打印banner(打印出来的Spring 1.X 或者Spring 2.X条幅)

    -> 创建应用上下文对象 -> 初始化失败分析器

    -> 关联springboot组件与应用上下文对象 -> 发送ApplicationContextInitializedEvent

    -> 加载source到context -> 发送ApplicationPreparedEvent

    -> 刷新上下文(调用Spring框架的DoRefresh方法)

    ->计时器停止计时 -> 发送ApplicationStartedEvent

    -> 调用框架启动扩展类 -> 发送ApplicationReadyEvent

    3、自动化装配

    收集配置文件中的配置工厂类

    加载组件工厂

    注册组件内定义bean

    Spring Boot的完整流程图

  • 相关阅读:
    js对象数组(JSON) 根据某个共同字段 分组
    一个 函数 用来转化esSearch 的range 条件
    关于 vuex 报错 Do not mutate vuex store state outside mutation handlers.
    android listview 重用view导致的选择混乱问题
    android SDK和ADT的更新
    Android中adb push和adb install的使用区别
    pycharm中添加扩展工具pylint
    su Authentication failure解决
    Putty以及adb网络调试
    有关android源码编译的几个问题
  • 原文地址:https://www.cnblogs.com/linlf03/p/12266225.html
Copyright © 2011-2022 走看看