tomcat启动(四)Catalina分析-server的init()方法

上一回load()方法解析讲到xml解析完成。

load()内部接下来会获取server

getServer().setCatalina(this);

这个server从createStartDigester()方法中可以看出getServer()返回的是StandardServer对象

具体请看:

Catalina.createDigester方法详细理解

1、接着执行Server.init方法

getServer().init();
看StandardServer类的关系
public final class StandardServer extends LifecycleMBeanBase implements Server
ζ

public abstract class LifecycleMBeanBase extends LifecycleBase
ζ

public abstract class LifecycleBase implements Lifecycle

Lifecycle.init()方法解释

准备组件启动。此方法应执行任何初始化所需的后期对象创建。
以下生命周期事件将按以下顺序触发:
  1. INIT_EVENT: On the successful completion of component initialization成功完成组件初始化

 2、由LifecycleBase重写其init()方法,方法里主要调用initInternal方法。这个方法是抽象方法

initInternal();

 3、LifecycleMBeanBase重写initInternal()方法

@Override
    protected void initInternal() throws LifecycleException {

        // If oname is not null then registration has already happened via
        // preRegister().
        if (oname == null) {
            mserver = Registry.getRegistry(null, null).getMBeanServer();
获取注册表,返回MBeanServer instance实例 oname
= register(this, getObjectNameKeyProperties()); } }
register(this, getObjectNameKeyProperties());
这个方法使子类轻松地注册。不使用MBeanServer实现JmxEnabled的其他组件。
注意:只有在initInternal()被调用之后才能使用此方法,并且在调用destroyInternal()之前。
.LifecycleMBeanBase.register(Object obj, String objectNameKeyProperties)
obj:向这个对象注册
objectNameKeyProperties:用于注册对象的对象名称的关键属性组件

 这个方法将Component实例注册到LifecycleMBeanBase

4、StandardServer.initInternal() 
super.initInternal();
// Register global String cache
        // Note although the cache is global, if there are multiple Servers
        // present in the JVM (may happen when embedding) then the same cache
        // will be registered under multiple names
        onameStringCache = register(new StringCache(), "type=StringCache");
// Register the MBeanFactory
        MBeanFactory factory = new MBeanFactory();
        factory.setContainer(this);
        onameMBeanFactory = register(factory, "type=MBeanFactory");
// Register the naming resources
        globalNamingResources.init();

// Initialize our defined Services
        for (int i = 0; i < services.length; i++) {
            services[i].init();
        }

 至于这个services是怎么来了,可以看到下面:service只有一个StandardService

Catalina.createDigester方法详细理解

接下来开始执行StandardService.init()

原文地址:https://www.cnblogs.com/gne-hwz/p/7724669.html