目录
-
- 启动流程分析
-
- [创建 SpringApplication](#创建 SpringApplication)
- [启动 run(String... args)](#启动 run(String... args))
启动流程分析
java
SpringApplication.run(App.class, args);
return new SpringApplication(primarySources).run(args);
创建 SpringApplication
java
SpringApplication(primarySources):
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//这个推断流程 001 篇 分析webServer里有详情 web应用返回WebApplicationType.SERVLET
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//获取Factories中配置的 BootstrapRegistryInitializer
this.bootstrapRegistryInitializers = new ArrayList<>(
getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
this.initializers = getSpringFactoriesInstances(ApplicationContextInitializer.class);
this.listeners = getSpringFactoriesInstances(ApplicationListener.class));
//推断main方法所在的Class
this.mainApplicationClass = deduceMainApplicationClass();
启动 run(String... args)
java
//获取Factories中配置的 SpringApplicationRunListener 并执行其 starting()
SpringApplicationRunListeners listeners = getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args).forEach(listener.starting(bootstrapContext))
//封装 args:这里会读取执行jar的时候添加的以 '--' 开头的 kay=value
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//读取配置 包括:系统,JVM,ServletContext,properties,yaml配置
//全部设置到 environment 中
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
//这一步我查资料应该是忽略Java解析BeanInfo的缓存 因为Spring使用的是ASM技术
configureIgnoreBeanInfo(environment);
//打印banner并返回
Banner printedBanner = printBanner(environment);
//根据类型返回上下文 001 有详细分析 AnnotationConfigServletWebServerApplicationContext
context = createApplicationContext();
context.applicationStartup = applicationStartup;
//利用ApplicationContextInitializer初始化Spring 上下文
//发布ApplicationContextInitializedEvent
//注册primarySources = run() 传入的配置类列表
//发布ApplicationContextPreparedEvent
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
//调用refresh(context) 这一步就是执行Spring bean创建的逻辑了
refreshContext(context);
//这一步是空的 给子类一个实现的机会
afterRefresh(context, applicationArguments);
//遍历执行之前 listeners started()
listeners.started(context, timeTakenToStartup);
//执行 ApplicationRunner.run(args);
//执行 CommandLineRunner.run(args.getSourceArgs());
//两个执行器只有 入参的区别 一个是解析之后的对象,一个是字符串
callRunners(context, applicationArguments);
//遍历执行之前 listeners ready()
listeners.ready(context, timeTakenToReady);