/**
* Bootstrap loader for Catalina. This application constructs a class loader
* for use in loading the Catalina internal classes (by accumulating all of the
* JAR files found in the "server" directory under "catalina.home"), and
* starts the regular execution of the container. The purpose of this
* roundabout approach is to keep the Catalina internal classes (and any
* other classes they depend on, such as an XML parser) out of the system
* class path and therefore not visible to application level classes.

这个类构造一个类加载器来加载Catalina内部类(通过在server目录下的catalina.home来找到所有的jar文件),和开始定期执行container容器。

这种回旋处理方法的目的是保持Catalina内部类(以及其依赖的任何其他类,如XML解析器)脱离系统类路径,因此对应用程序级别不可见

入口main方法

在进入main之前会前执行static静态模块代码:主要是设置catalina.home和catalina.base的路径

public static void main(String args[]) {
bootstrap初始化init()
设置守护进程daemon = bootstrap;
识别启动bootstrap时传递的参数command = args[args.length - 1];//start
daemon.setAwait(true);
daemon.load(args);
daemon.start();}

上面damemon守护进程的方法setAwait(),load(),start()其实是反射调用的org.apache.catalina.startup.Catalina类的方法

到这里其实就进入到Catalina类的。

--------------------------Bootstrap类解析完毕------下面是对init方法解释---------------------------------------------------------

初始化init方法(初始化守护进程):

在这个方法里主要流程

1、初始化类加载器initClassLoaders()----------commonLoader,catalinaLoader,sharedLoade会先加载catalina.base/conf/catalina.propertises配置文件

然后读取common.loader键所对应的值

Catalina.properties文件下
common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar"

补充下:

commonLoader再定义时使用ClassLoader定义,但是创建返回的是URLClassLoader。公有类定义,子类返回,这个思路值得借鉴

public class URLClassLoader extends SecureClassLoader implements Closeable

URLClassLoader继承SecureClassLoader

public class SecureClassLoader extends ClassLoader

SecureClassLoader继承ClassLoader

ClassLoader commonLoader = null;
ClassLoader catalinaLoader = null;
ClassLoader sharedLoader = null;

private void initClassLoaders() {
try {
commonLoader = createClassLoader("common", null);
if( commonLoader == null ) {
// no config file, default to this loader - we might be in a 'single' env.
commonLoader=this.getClass().getClassLoader();
}
catalinaLoader = createClassLoader("server", commonLoader);
sharedLoader = createClassLoader("shared", commonLoader);

createClassLoader()

获取common.loader键对应的值
String value = CatalinaProperties.getProperty(name + ".loader"); String[] repositoryPaths = getPaths(value);
。。。省略部分代码
for (String repository : repositoryPaths) {
// Check for a JAR URL repository
try {
@SuppressWarnings("unused")
URL url = new URL(repository);
repositories.add(
new Repository(repository, RepositoryType.URL));
continue;
} catch (MalformedURLException e) {
// Ignore
} // Local repository
if (repository.endsWith("*.jar")) {
repository = repository.substring
(0, repository.length() - "*.jar".length());
repositories.add(
new Repository(repository, RepositoryType.GLOB));
} else if (repository.endsWith(".jar")) {
repositories.add(
new Repository(repository, RepositoryType.JAR));
} else {
repositories.add(
new Repository(repository, RepositoryType.DIR));
}
}
return ClassLoaderFactory.createClassLoader(repositories, parent);最后调用这个方法将repositories内存放的类和资源的路径绑定到commonLoader。这里返回的是new URLClassLoader

createClassLoader()方法获取到

common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar"

接下来会判断这个字符串是以什么结尾

${catalina.base}/lib
${catalina.home}/lib

/lib是目录 commonLoader会加载整个目录下的资源,包括所有clsss、jar包及其它类型资源

${catalina.base}/lib/*.jar
${catalina.home}/lib/*.jar

表示整个目录下所有jar包资源,仅仅是.jar后缀的资源

解释下RepositoryType这是一个枚举类型,定义再类org.apache.catalina.startup.ClassLoaderFactory内部

public static enum RepositoryType {
DIR,表示整个目录下的资源,包括所有clsss、jar包及其它类型资源。
GLOB,表示整个目录下所有jar包资源,仅仅是.jar后缀的资源。
JAR,表示单个jar包资源。
URL表示网络上得某个jar包资源
}

2、为当前线程设置classLoader

Thread.currentThread().setContextClassLoader(catalinaLoader);catalinaLoader其实就是commonLoader

用静态类SecurityClassLoad预加载类资源

SecurityClassLoad.securityClassLoad(catalinaLoader);

securityClassLoad(ClassLoader loader, boolean requireSecurityManager)  {   
。。。。。略。。。。。。。。。。
     loadCorePackage(loader);
loadCoyotePackage(loader);
loadLoaderPackage(loader);
loadRealmPackage(loader);
loadServletsPackage(loader);
loadSessionPackage(loader);
loadUtilPackage(loader);
loadValvesPackage(loader);
loadWebResourcesPackage(loader);
loadJavaxPackage(loader);
loadConnectorPackage(loader);
loadTomcatPackage(loader);
}

3、初始化org.apache.catalina.startup.Catalina利用反射调用它的setParentClassLoader设置sharedLoader;(设置的parentClassLoader的原因和用处暂不完全清楚,看源码估计会在server.xml加载部分会使用到)

参考资源

Tomcat内核之类加载器工厂

具体源码解析:http://blog.csdn.net/u011545486/article/details/52002626

tomcat启动(二)org.apache.catalina.startup.Bootstrap分析的更多相关文章

  1. IDEA tomcat启动异常 org.apache.catalina.startup.ContextConfig parseWebXml

    启动Tomcat发现有异常,总是无法启动,具体的异常日志为下图 具体的解决方法为:在tomcat的conf/content.xml中加上<Loader delegate="true&q ...

  2. Tomcat7.0 start Could not find the main class: org.apache.catalina.startup.Bootstrap.

    java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory at org.apache.catalina.startup.Bo ...

  3. tomcat启动 报org.apache.catalina.LifecycleException异常

    java 服务器 tomcat启动 报org.apache.catalina.LifecycleException异常 异常代码如下: [2018-05-10 04:45:08,856] Artifa ...

  4. Could not find the main class: org.apache.catalina.startup.Bootstrap. Program will exit.

    出现此异常原因是jdk环境变量未配置正确

  5. 启动Tomcat服务时,出现org.apache.catalina.startup.VersionLoggerListener报错

    启动Tomcat服务时,出现org.apache.catalina.startup.VersionLoggerListener报错解决办法:打开Tomcat安装后目录,进入conf文件夹,找到配置文件 ...

  6. MyEclipse启动Tomcat报错:Could not find the main class: org.apache.catalina.startup

    问题描述 Could not find the main class:org.apache.catalina.startup.Bootstrap. Program will exit 问题原因 主要原 ...

  7. 现网环境业务不影响,但是tomcat启动一直有error日志,ERROR org.apache.catalina.startup.ContextConfig- Unable to process Jar entry [module-info.class] from Jar [jar:file:/home/iufs/apache-tomcat/webapps/iufs/WEB-INF/lib/asm

    完整的错误日志信息: 2019-03-19 15:30:42,021 [main] INFO org.apache.catalina.core.StandardEngine- Starting Ser ...

  8. Tomcat无法启动:org.apache.catalina.LifecycleException: Failed to start component 问题解决

    问题如下:需要使用到数据库mysql,于是将mysql-connector-java-5.1.30-bin.jar的数据库驱动复制到WEE-INF/lib目录下.点击运行,但是服务器无法启动. 控制台 ...

  9. java.lang.ClassNotFoundException: org.apache.catalina.startup.VersionLoggerListener

    解决办法 找到Tomcat配置文件server.xml   apache-tomcat-7.0.57/conf 将<Listener className="org.apache.cat ...

随机推荐

  1. 基于Verilog HDL的二进制转BCD码实现

    在项目设计中,经常需要显示一些数值,比如温湿度,时间等等.在数字电路中数据都是用二进制的形式存储,要想显示就需要进行转换,对于一个两位的数值,对10取除可以得到其十位的数值,对10取余可以得到个位的数 ...

  2. linux信号量(转载)

    本文转载自http://blog.csdn.net/qinxiongxu/article/details/7830537 信号量一.什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个时刻只 ...

  3. Mysql工作記錄之修改默認存儲引擎及重設root用戶密碼

    1>修改默認存儲引擎方法 修改配置文件,然後重啟mysql服務: [root@CHW mysql]# cat /etc/my.cnf                            [my ...

  4. 最佳编程字体:M+

    英文原文:The Best Font for Programming: M+ 程序员的最佳等宽字体是 M+. 一个偶然机会遇到了这个字体,在命运多舛的  kod 编辑器体验了.这个字体非比寻常,我坚持 ...

  5. Python学习-25.Python中的分数

    在Python中,不止有浮点数(float),而且还有分数(Fraction)这个类型. 要使用分数,必须引入一个模块. import fractions 然后就可以声明一个分数了 x = fract ...

  6. 疑难杂症--SQL SERVER 2012下数据库内存异常回收

    --=================================================================== --背景: 在一台SQL SERVER 2012 SP1(1 ...

  7. WebAPI+Html跨域时对session的支持

    1.Global.asax中添加对Session的支持,重新Init方法: public override void Init() { this.PostAuthenticateRequest += ...

  8. C#HttpUtility.UrlEncode 大写问题

    工作上和另一个公司对接,调对方的api需要用到md5加密,加密前要使用HttpUtility.UrlEncode,对方接口一直返回验证错误,定位了问题发现是中文编码使用HttpUtility.UrlE ...

  9. BitAdminCore框架应用篇:(一)使用Cookiecutter创建应用项目

      框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookiecutter-bitadmin-core 一.简介 1.Coo ...

  10. c# WPF客户端调用WebAPI并转换成List

    利用HttpClient.JsonConvert实现. 引用Newtonsoft.Json.dll和System.Net.Http. 举个例子:从webapi中获取设备列表. public parti ...