SpringBoot流程图
2023-08-22 17:37:19 1 举报
SpringBoot流程图
作者其他创作
大纲/内容
根据应用类型创建Spring容器
发布ApplicationContextInitializedEvent
发布ApplicationStartingEvent
ServletWebServerApplicationContext.createWebServer
refreshContext(context);
SpringApplicationRunListeners listeners = getRunListeners(args);
// 如果想自定义端口号,可以提供一个TomcatConnectorCustomizer去设置端口号优先级最高for (TomcatConnectorCustomizer customizer : this.tomcatConnectorCustomizers) { customizer.customize(connector);}
根据是否存在某个类推测this.webApplicationType = WebApplicationType.deduceFromClasspath();1. 如果项目依赖中存在org.springframework.web.reactive.DispatcherHandler,并且不存在org.springframework.web.servlet.DispatcherServlet,那么应用类型为WebApplicationType.REACTIVE2. 如果项目依赖中不存在org.springframework.web.reactive.DispatcherHandler,也不存在org.springframework.web.servlet.DispatcherServlet,那么应用类型为WebApplicationType.NONE3. 否则,应用类型为WebApplicationType.SERVLET
配置类解析、beanpostprocessror添加、单例bean创建.....
getTomcatWebServer
从spring.factories中获取ApplicationListener对象放入listeners
context = createApplicationContext();
以tomcat容器为例
AbstractApplicationContext.onRefresh
this.bootstrapRegistryInitializers = new ArrayList<>( getSpringFactoriesInstances(BootstrapRegistryInitializer.class));setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));this.mainApplicationClass = deduceMainApplicationClass();
利用ApplicationContextInitializer初始化Spring容器applyInitializers(context);
ServletWebServerApplicationContext.onRefresh
this.webServer = factory.getWebServer(getSelfInitializer()); 创建容器
run
从spring.factories中获取ApplicationContextInitializer对象放入initializers
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
将run()的参数封装为DefaultApplicationArguments对象,就是args,启动时通过-D去指定
bootstrapContext.close(context);
从Spring容器中获取ApplicationRunner和CommandLineRunner,并执行其run()
从spring.factories中获取BootstrapRegistryInitializer对象放入bootstrapRegistryInitializers
发布ApplicationStartedEvent事件,表示Spring容器已经启动
获取容器中的ServletWebServerFactory,如果是Tomcat容器 则为TomcatServletWebServerFactory
AbstractApplicationContext.refresh
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
customizeConnector
initialize
创建引导启动器DefaultBootstrapContext,类似一个ApplicationContext,可以往里面添加一些对象,依次调用bootstrapRegistryInitializers中的initialize方法进行初始化
this.tomcat.start();
推测出Main类(main()方法所在的类),获取当前运行的堆栈,拿出main方法的类型
关闭DefaultBootstrapContext
准备Environment
new SpringApplication(primarySources).run(args);
刷新Spring容器,会解析配置类、扫描、启动WebServer
ServletWebServerFactory factory = getWebServerFactory();
new SpringApplication(primarySources)
推测web应用类型(NONE、REACTIVE、SERVLET)
从spring.factories中获取SpringApplicationRunListener对象,默认会拿到一个EventPublishingRunListener,它会启动过程的各个阶段发布对应的ApplicationEvent事件
注册primarySources类,就是run方法存入进来的配置类
0 条评论
下一页