---
title: "tomcat项目启动过程"
author: "nj_link"
date: "2015-04-09T17:47:03Z"
tags: []
categories: ["编程语言"]
slug: "tomcat"
draft: false
lastmod: "2015-04-09T17:47:03Z"
id: 2200673
url: "https://nj-link.iteye.com/blog/2200673"
---

1、startup 读取配置文件配置的jar，lib下的jar，环境变量配置的jar。
2、catalina 读取tomcat内置的用户管理界面,读取配置tomcat-user.xml。
3、读取server.xml，根据层级关系设置到services对象中。每个节点都有自己的listener(用来根据配置，不同节点执行不同的代码)。
4、执行service下配置的Listener
5、执行Connector初始化
6、初始化流程结束，开始启动流程，注册MBean到JMX
7、StandardService start 执行本身listener
8、StandardEngine start 执行本身listener .读取/conf/tomcat5-mbeans.xml
9、StandardHost start 执行本身listener
10、StandardPipeline start 执行本身listener
11、HostConfig读取host-manager.xml, manager.xml
12、发布war工程文件
13、启动webapp的工程
14、listeners->filter->servlet，代码如下：

```java

        try {
            
            // Create context attributes that will be required
            if (ok) {
                postWelcomeFiles();
            }
            
            // Set up the context init params
            mergeParameters();

            if (ok) {
                // Notify our interested LifecycleListeners
                lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
            }
            
            // Configure and call application event listeners
            if (ok) {
                if (!listenerStart()) {
                    log.error( "Error listenerStart");
                    ok = false;
                }
            }
            
            try {
                // Start manager
                if ((manager != null) && (manager instanceof Lifecycle)) {
                    ((Lifecycle) getManager()).start();
                }
    
                // Start ContainerBackgroundProcessor thread
                super.threadStart();
            } catch(Exception e) {
                log.error("Error manager.start()", e);
                ok = false;
            }

            // Configure and call application filters
            if (ok) {
                if (!filterStart()) {
                    log.error( "Error filterStart");
                    ok = false;
                }
            }
            
            // Load and initialize all "load on startup" servlets
            if (ok) {
                loadOnStartup(findChildren());
            }
            
        } finally {
            // Unbinding thread
            unbindThread(oldCCL);
        }

```