Tomcat生命周期统一接口——Lifecycle

Tomcat生命周期统一接口——Lifecycle


你可以先看看 Tomcat 体系架构

为什么要有 Lifecycle

在这里插入图片描述
如上图,Tomcat 的一个 Server 里包含了这么多组件,特别是 Container,一层一层的。

试想,如果我们没有一个东西来管理这些组件的生命周期,当内层组件 start 了,外层组件却还没 start;当外层组件 stop 了,内层组件还没 stop。你觉得这样符合逻辑吗?确实不符合逻辑,这样很容易发生异常。

所以,Lifecycle 就是为了管理整个 Tomcat 各个组件的生命周期。
Lifecycle定义了一些状态常量和几个方法,主要方法是init,start,stop三个方法。
在这里插入图片描述

Server 与 Lifecycle

Server 是上面组件图的最外层,我们先来好好看看 Server:

Server 组件的 init 负责遍历调用其包含所有的 Service 组件的 init 方法。

这一点我们可以通过下文证明。

Server 继承自 Lifecycle,也是一个 接口:
在这里插入图片描述
下面的是 Server 的唯一实现类 StandardServer:
在这里插入图片描述
LifecyMBeanBase 继承了 LifecycleBase:
在这里插入图片描述
LifecycleBase 是继承了 Lifecycle 的骨架类:
在这里插入图片描述

LifecycleBase

在这里插入图片描述
initInternal() 在 LifecycleBase 里并没有实现:
在这里插入图片描述
在 StandardServer 里,它被重写了:
在这里插入图片描述
这证明了我们前面说的:
Server 组件的 init 负责遍历调用其包含所有的 Service 组件的 init 方法。

发布了130 篇原创文章 · 获赞 233 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44367006/article/details/104068255
今日推荐