文章插图
5、启动tomcat容器
创建webServer后在refresh()方法中的finishRefresh()完成tomcat的启动 。
最终会调用 TomcatWebServer中的start()方法启动tomcat容器 。
protected void finishRefresh() {// Clear context-level resource caches (such as ASM metadata from scanning).clearResourceCaches();// Initialize lifecycle processor for this context.initLifecycleProcessor();// Propagate refresh to lifecycle processor first.// 启动tomcatgetLifecycleProcessor().onRefresh();// Publish the final event.publishEvent(new ContextRefreshedEvent(this));// Participate in LiveBeansView MBean, if active.if (!NativeDetector.inNativeImage()) {LiveBeansView.registerApplicationContext(this);}}@Overridepublic void onRefresh() {startBeans(true);this.running = true;}private void startBeans(boolean autoStartupOnly) {Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();Map<Integer, LifecycleGroup> phases = new TreeMap<>();lifecycleBeans.forEach((beanName, bean) -> {if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {int phase = getPhase(bean);phases.computeIfAbsent(phase,p -> new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly)).add(beanName, bean);}});if (!phases.isEmpty()) {// 遍历启动phases.values().forEach(LifecycleGroup::start);}}public void start() {if (this.members.isEmpty()) {return;}if (logger.isDebugEnabled()) {logger.debug("Starting beans in phase " + this.phase);}Collections.sort(this.members);for (LifecycleGroupMember member : this.members) {doStart(this.lifecycleBeans, member.name, this.autoStartupOnly);}}private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {Lifecycle bean = lifecycleBeans.remove(beanName);if (bean != null && bean != this) {String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);for (String dependency : dependenciesForBean) {doStart(lifecycleBeans, dependency, autoStartupOnly);}if (!bean.isRunning() &&(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {if (logger.isTraceEnabled()) {logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");}try {bean.start();}catch (Throwable ex) {throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);}if (logger.isDebugEnabled()) {logger.debug("Successfully started bean '" + beanName + "'");}}}}//WebServerStartStopLifecycle 中start() 方法的实现public void start() {this.weServerManager.start();this.running = true;}void start() {this.handler.initializeHandler();this.webServer.start();this.applicationContext.publishEvent(new ReactiveWebServerInitializedEvent(this.webServer, this.applicationContext));}// 完成对tomcat的启动@Overridepublic void start() throws WebServerException {synchronized (this.monitor) {if (this.started) {return;}try {addPreviouslyRemovedConnectors();Connector connector = this.tomcat.getConnector();if (connector != null && this.autoStart) {performDeferredLoadOnStartup();}checkThatConnectorsHaveStarted();this.started = true;logger.info("Tomcat started on port(s): " + getPortsDescription(true) + " with context path '"+ getContextPath() + "'");}catch (ConnectorStartFailedException ex) {stopSilently();throw ex;}catch (Exception ex) {PortInUseException.throwIfPortBindingException(ex, () -> this.tomcat.getConnector().getPort());throw new WebServerException("Unable to start embedded Tomcat server", ex);}finally {Context context = findContext();ContextBindings.unbindClassLoader(context, context.getNamingToken(), getClass().getClassLoader());}}}
同博客: SpringBoot内置Tomcat启动原理源码分析 - sun丶牧羊人 - 博客园推荐阅读
- 国外服务器部署springboot 项目 出现时区问题
- JavaScript 内置对象之——Date,看完你会更清晰
- 效率这不就提上来了嘛!SpringBoot+flowable快速实现工作流
- SpringBoot 如何使用同步锁
- Springboot使用OkHttp实现微信支付API-V3签名、证书的管理和使用
- 不用某度搜索和 XX 管家,这些应用在 Windows 内置商店就能下载到
- python初学者必须吃透的这些内置函数
- 开源的springboot+thymeleaf后台架构,程序员用了都点赞
- 68 个 Python 内置函数详解
- 快速打造属于自己的 PE 系统,内置大量插件包