标签:lifecycle href 统一 exception info 知识 move protect 执行
1. 本系列文章基于 Tomcat9
2. 前置知识:servlet
先来看看容器的定义,Tomcat 源码注释如下:
/**
* A <b>Container</b> is an object that can execute requests received from
* a client, and return responses based on those requests. A Container may
* optionally support a pipeline of Valves that process the request in an
* order configured at runtime, by implementing the <b>Pipeline</b> interface
* as well.
* .....
*/
简单说就是执行客户端的 request,并返回 response。
这四种容器含义如下:
那这些容器如何组合在一起的呢? 这些接口都继承了 Container 接口,有设置父容器、子容器的方法
public interface Container extends Lifecycle {
public void setName(String name);
public Container getParent();
public void setParent(Container container);
public void addChild(Container child);
public void removeChild(Container child);
public Container findChild(String name);
}
每个容器都维护着自己的父子容器
protected final HashMap<String, Container> children = new HashMap<>();
protected Container parent = null;
Mapper 类负责 Url 和 servlet 之间的映射,具体过程可以看 Mapper.map()
LifeCycle 抽象了组件的生命周期,父容器驱动子容器,达到了一键式启停。做到了统一的管理、方便的调用,同时代码逻辑清晰易懂,值得学习。
下面是 LifeCycle 的接口的方法:
public void init() throws LifecycleException;
public void start() throws LifecycleException;
public void stop() throws LifecycleException;
public void destroy() throws LifecycleException;
public LifecycleState getState();
public String getStateName();
public void addLifecycleListener(LifecycleListener listener);
public void removeLifecycleListener(LifecycleListener listener);
public LifecycleListener[] findLifecycleListeners();
/** 下面是生命周期事件,用于扩展生命周期对应的操作*/
BEFORE_INIT_EVENT
AFTER_INIT_EVENT
START_EVENT
BEFORE_START_EVENT
AFTER_START_EVENT
STOP_EVENT
BEFORE_STOP_EVENT
AFTER_STOP_EVENT
AFTER_DESTROY_EVENT
BEFORE_DESTROY_EVENT
PERIODIC_EVENT
CONFIGURE_START_EVENT
CONFIGURE_STOP_EVENT
标签:lifecycle href 统一 exception info 知识 move protect 执行
原文地址:https://www.cnblogs.com/walterlee/p/13176089.html