try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
//创建应用上下文
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
}
在创建应用上下文的过程中,此处根据webApplicationType
属性判断来决定创建具体类型的ApplicationContext
,而webApplicationType
属性在第一阶段的SpringApplication
实例创建的时候进行获取。SpringBoot
将应用程序分为三种类型NONE
(非web类型应用)、SERVLET
(以嵌入web服务器启动的web应用)、REACTIVE
(响应式web应用程序)。根据获取到的应用类型创建对应的ApplicationContext
。
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch(this.webApplicationType) {
//创建AnnotationConfigServletWebServerApplicationContext
case SERVLET:
contextClass = Class.forName(“org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext”);
break;
case REACTIVE:
contextClass = Class.forName(“org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext”);
break;
default:
contextClass = Class.forName(“org.springframework.context.annotation.AnnotationConfigApplicationContext”);
}
} catch (ClassNotFoundException var3) {
throw new IllegalStateException(“Unable create a default ApplicationContext, please specify an ApplicationContextClass”, var3);
}
}
return (ConfigurableApplicationContext)BeanUtils.instantiateClass(contextClass);
}
这里获取到的应用类型为SERVLET
,因此会创建AnnotationConfigServletWebServerApplicationContext
上下文。
而AnnotationConfigServletWebServerApplicationContext
类继承了ServletWebServerApplicationContext
,而这个类是最终继承了AbstractApplicationContext
。
5、刷新应用上下文
private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}
protected void refresh(ApplicationContext applicationContext) {
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
((AbstractApplicationContext) applicationContext).refresh();
}
由下图可知,实际进行应用上下文刷新的由之前创建的ServletWebServerApplicationContext
进行。
ServletWebServerApplicationContext
应用上下文中完成refresh
,我们可以看到refresh
操作是通过实现父类AbstractApplicationContext
操作来进行的。,
@Override
public final void refresh() throws BeansException, IllegalStateException {
try {
super.refresh();
}
catch (RuntimeException ex) {
stopAndReleaseWebServer();
throw ex;
}
}
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException(“Unable to start web server”, ex);
}
}
在onRefresh
操作中,实际在ServletWebServerApplicationContext
中进行属于该应用上下文业务相关的操作,即创建WebServer
实例
6、创建WebServer
实例,启动Tomcat实例
ServletWebServerApplicationContext
中定义了onRefresh
操作,用以创建WebServer
实例。
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException(“Unable to start web server”, ex);
}
}
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
//创建webServer实例
this.webServer = factory.getWebServer(getSelfInitializer());
}
else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException(“Cannot initialize servlet context”, ex);
}
}
initPropertySources();
}
TomcatServletWebServerFactory
用于实现获取WebServer
实例。
@Override
public WebServer getWebServer(ServletContextInitializer… initializers) {
//创建Tomcat实例
Tomcat tomcat = new Tomcat();
//创建Tomcat工作目录
-
File baseDir = (this.baseDirectory != null) ? this.baseDirectory
- createTempDir(“tomcat”);
tomcat.setBaseDir(baseDir.getAbsolutePath());
//创建连接对象(Connector是Tomcat重要组件,主要负责处理客户端连接,以及请求处理,这里简单解释下)
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
//准备tomcat context
prepareContext(tomcat.getHost(), initializers);
//返回WebServer实现TomcatWebServer
return getTomcatWebServer(tomcat);
}
在返回TomcatWebServer
实例过程中,进行TomcatWebServer
初始化操作,进而完成tomcat
实例的启动流程。
public TomcatWebServer(Tomcat tomcat, boolean autoStart) {
Assert.notNull(tomcat, “Tomcat Server must not be null”);
this.tomcat = tomcat;
this.autoStart = autoStart;
initialize();
}
private void initialize() throws WebServerException {
logger.info("Tomcat initialized with port(s): " + getPortsDescription(false));
synchronized (this.monitor) {
try {
addInstanceIdToEngineName();
Context context = findContext();
context.addLifecycleListener((event) -> {
if (context.equals(event.getSource()) && Lifecycle.START_EVENT.equals(event.getType())) {
// Remove service connectors so that protocol binding doesn’t
// happen when the service is started.
removeServiceConnectors();
}
});
// Start the server to trigger initialization listeners
this.tomcat.start();
// We can re-throw failure exception directly in the main thread
rethrowDeferredStartupExceptions();
try {
ContextBindings.bindClassLoader(context, context.getNamingToken(), getClass().getClassLoader());
}
catch (NamingException ex) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
由于篇幅限制,小编在此截出几张知识讲解的图解
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
munity.csdnimg.cn/images/e5c14a7895254671a72faed303032d36.jpg" alt=“img” style=“zoom: 33%;” />
最后
由于篇幅限制,小编在此截出几张知识讲解的图解
[外链图片转存中…(img-wPqovmcn-1713321893640)]
[外链图片转存中…(img-NWGzXCmA-1713321893640)]
[外链图片转存中…(img-a5JBw5BV-1713321893640)]
[外链图片转存中…(img-AAZGP0UM-1713321893640)]
[外链图片转存中…(img-CytYTrAT-1713321893641)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!