5.SpringBoot核心源码-启动类源码分析

概述

  SpringBoot核心源码-启动类源码分析

技巧

  如何给外部源码加注释,想要在源码中添加自己的注释,会弹出 file is read only,代码只读

  • 方式一:下载框架源码,安装对应的环境,编译,然后即可注释修改保存
  • 方式二:idea插件 private notes ,不影响调试,方便简单

在这里插入图片描述

spring boot 如何启动应用程序

run方法里面核心逻辑 SpringApplicaiton.run(xxx.class,args)

# org.springframework.boot.SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class<?>...)
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
	this.resourceLoader = resourceLoader;
	Assert.notNull(primarySources, "PrimarySources must not be null");
	# 存储项目启动类
	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
	// 判断应用是servlet还是reactive应用
	this.webApplicationType = WebApplicationType.deduceFromClasspath();
	// 从spring.factories中读取key为Bootstrap
	this.bootstrapRegistryInitializers = new ArrayList<>(
			getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
	// 设置初始化器		
	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
	// 设置监听器
	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
	// 推断并配置项目main函数的主程序启动类
	this.mainApplicationClass = deduceMainApplicationClass();
}
public ConfigurableApplicationContext run(String... args) {
	Startup startup = Startup.create();
	if (this.registerShutdownHook) {
		SpringApplication.shutdownHook.enableShutdownHookAddition();
	}
	DefaultBootstrapContext bootstrapContext = createBootstrapContext();
	ConfigurableApplicationContext context = null;
	configureHeadlessProperty();
	SpringApplicationRunListeners listeners = getRunListeners(args);
	listeners.starting(bootstrapContext, this.mainApplicationClass);
	try {
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
		ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
		Banner printedBanner = printBanner(environment);
		context = createApplicationContext();
		context.setApplicationStartup(this.applicationStartup);
		prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
		refreshContext(context);
		afterRefresh(context, applicationArguments);
		startup.started();
		if (this.logStartupInfo) {
			new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), startup);
		}
		listeners.started(context, startup.timeTakenToStarted());
		callRunners(context, applicationArguments);
	}
	catch (Throwable ex) {
		throw handleRunFailure(context, ex, listeners);
	}
	try {
		if (context.isRunning()) {
			listeners.ready(context, startup.ready());
		}
	}
	catch (Throwable ex) {
		throw handleRunFailure(context, ex, null);
	}
	return context;
}

结束

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 18:04:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 18:04:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 18:04:01       58 阅读
  4. Python语言-面向对象

    2024-07-13 18:04:01       69 阅读

热门阅读

  1. 4层负载均衡和7层负载均衡

    2024-07-13 18:04:01       20 阅读
  2. 大话C语言:第31篇 指针和数组的关系

    2024-07-13 18:04:01       22 阅读
  3. 算法提高第二章 线段树基础

    2024-07-13 18:04:01       18 阅读
  4. django orm中value和value_list以及转成list

    2024-07-13 18:04:01       21 阅读
  5. C# .Net Core Zip压缩包中文名乱码的解决方法

    2024-07-13 18:04:01       22 阅读
  6. live555关于RTSP协议交互流程

    2024-07-13 18:04:01       16 阅读
  7. EXPORT_SYMBOL

    2024-07-13 18:04:01       24 阅读
  8. 【车载开发系列】汽车开发常见概念理解

    2024-07-13 18:04:01       19 阅读