不知道大家以前是否经历过SSM这种框架组合,将项目打包成war,部署到Tomcat容器中,进行启动。也就是说,Tomcat服务器的启动带动了IOC容器的加载。
SpringBoot内置了Tomcat,那就是相反,IOC容器的加载,伴随着Tomcat服务的启动。
我们就SpringBoot源码,来分析一下SpringBoot内置的Tomcat是如何启动的。
首先,从主函数入口:
SpringApplication.run(AppRun.class, args)
refreshContext(context)
refresh(context)
applicationContext.refresh()
super.refresh()
进入refresh方法
前面谈到IOC的时候,讲过这个方法,这个refresh方法,使用了模版设计,可以提高代码复用性和扩展性。
不如onRefresh方法,是个空方法。
来到onRefresh的实现类中