1.将一个工程打包,然后查看jar包中的文件有一个MANIFEST.MF文件:
Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: postgresql-demo
Implementation-Version: 1.0-SNAPSHOT
Start-Class: com.lyr.postgresql.PostgresqlDemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.3.10.RELEASE
Created-By: Maven Jar Plugin 3.2.0
Main-Class: org.springframework.boot.loader.JarLauncher
2.当使用java -jar 去启动这个jar包时从Main-Class实例化指定的JarLauncher类进开始:
在JarLauncher实例化时会创建LaunchedURLClassLoader加载器类,通过它来加载指定文件夹下的类,并找到META-INF/MANIFEST.MF下的Start-Class配置,启动我们自己服务写的main方法,开始启动springboot应用。
- org.springframework.boot.loader.JarLauncher主要做的事:
1、最主要就是为了生成一个自定义的LaunchedURLClassLoader类加载器,指定好这个类加载器负责BOOT-INF/classes/和BOOT-INF/lib/下的所有jar包。然后再把这个类加载器放在上下文加载器里(Thread.currentThread().setContextClassLoader(classLoader);),以保证整个Spring启动过程中优先使用这个上下文加载器来加载类。
2、找到META-INF/MANIFEST.MF下的Start-Class配置(即:Application.class),通过反射启动我们业务代码里的Application.class,并通过反射启动Application.class的main方法,拉起整个Spring容器。
3.接下来就是,SpringApplication.run(PostgresqlDemoApplication.class, args)的启动过程:
主要流程为:
- 初始化SpringApplication,其中通过加载spring-boot、spring-boot-autoconfigure、spring-beans包下的spring.factory文件初始化基础的类
- 开始启动计时
- 启动应用监听
- 加载应用配置文件,准备环境
- 打印应用banner
- 创建应用上下文
- 准备上下文
- 刷新上下文,扫描三方应用的spring.factory在此步完成,扫描加载bean,启动web服务器
- 最后刷新上下文
- 停止启动计时
- 启动所有上下文的监听
- 返回应用上下文对象,启动完成
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
2.1 资源加载
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
2.2 将主类放入linkedHastSet当中
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
2.3 应用类型的选择
this.webApplicationType = WebApplicationType.deduceFromClasspath();
2.4 设置初始化(构造)器
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
2.5 设置(应用)监听器
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
2.6 配置应用的主方法所在类
this.mainApplicationClass = deduceMainApplicationClass();
}
public ConfigurableApplicationContext run(String... args) {
3.1 计时器开始
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
3.2 配置参数
configureHeadlessProperty();
3.3 配置运行监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
3.4 监听器开始工作
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
3.5准备环境--监听器和应用参数
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
3.6配置忽略的Bean信息
configureIgnoreBeanInfo(environment);
3.7打印图案
Banner printedBanner = printBanner(environment);
3.8 创建应用上下文
context = createApplicationContext();
3.9 异常报告
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
3.10 准备上下文
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
3.11 刷新上下文
refreshContext(context);
3.12 刷新上下文善后
afterRefresh(context, applicationArguments);
3.13 计时器结束
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
3.14 启动所有监听器
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
3.15 处理运行失败
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
3.16 将应用上下文传递给每一个监听器
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}