SpringBoot (3) WebServerApplicationContext

SpringBoot (3) WebServerApplicationContext

SpringBoot版本

SpringBoot 2.1.6

WebServerApplicationContext

在run方法中,通过反射创建了AnnotationConfigServletWebServerApplicationContext

 public ConfigurableApplicationContext run(String... args) {
		 ...
         ConfigurableApplicationContext context = null;
		 ...
		try {
			...
			context = createApplicationContext();
			...
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			...
		}
		catch (Throwable ex) {
			...
		}
		...
		return context;
	}

prepareContext

准备context的环境,应用相关的后置处理器,调用ApplicationContextInitializer的initialize初始化 ,加载资源,加载相关bean

refreshContext

refreshContext使用了refresh,调用了AbstractApplicationContext的refresh

private void refreshContext(ConfigurableApplicationContext context) {
		refresh(context);
		if (this.registerShutdownHook) {
			try {
				context.registerShutdownHook();
			}
			catch (AccessControlException ex) {
				// Not allowed in some environments.
			}
		}
	}

refresh属于Spring framework中的操作

AutoConfigurationImportSelector

通过ConfigurationClassPostProcessor来扫描注解@ComponentScan,@Import(AutoConfigurationImportSelector)

创建server

在创建了AnnotationConfigServletWebServerApplicationContext后的refresh过程中OnRefresh,创建了WebServer

@Override
protected void onRefresh() {
   super.onRefresh();
   try {
      createWebServer();
   }
   catch (Throwable ex) {
      throw new ApplicationContextException("Unable to start web server", ex);
   }
}

通过ServletWebServerFactory创建了WebServer,在通过ServletContextInitializer 的接口做init。

public interface ServletContextInitializer {

   /**
    * Configure the given {@link ServletContext} with any servlets, filters, listeners
    * context-params and attributes necessary for initialization.
    * @param servletContext the {@code ServletContext} to initialize
    * @throws ServletException if any call against the given {@code ServletContext}
    * throws a {@code ServletException}
    */
   void onStartup(ServletContext servletContext) throws ServletException;

}

启动server

重写了finishRefresh,开始启动server

@Override
protected void finishRefresh() {
   super.finishRefresh();
   WebServer webServer = startWebServer();
   if (webServer != null) {
      publishEvent(new ServletWebServerInitializedEvent(webServer, this));
   }
}

小结

ApplicationContext的类型是可以通过参数控制,但是常用的也就是web和非web,还有一种是Reactive,可以看出SpringBoot完全是靠注解走遍所有流程,文中已经省略了涉及的Spring框架,如果觉得不明白,还是去先看明白Spring框架,再看这篇水文。

通过对Spring框架的扩展,SpringBoot在执行过程中context有了,servlet也有了,我们所需要的资源都ok了。

OK,又水一篇

key word

AnnotationConfigServletWebServerApplicationContext

主目录

SpringBoot框架及源码分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eric.Cui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值