一 web容器类加载机制
1 tomcat类加载机制
Tomcat中的类加载策略和JDK中的委托模型略有不同。Tomcat Server在启动的时候将构造一个ClassLoader树,以保证模块的类库是私有的,ClassLoader结构如下:
|
System
|
Common
/ \
Catalina Shared
/ ... ... \
webapp1 webappN
1)Bootstrap ClassLoader:
2)System ClassLoader:
3)Common ClassLoader:
它负责加载Tomcat本身和所有的web应用都能看到的类。通常,应用的类不应该由他加载。
$CATALINA_HOME/common/classes,$CATALINA_HOME/commons/endorsed和$CATALINA_HOME/common/lib下的类都由这个加载器加载。缺省的,包括:
ant.jar - Apache Ant.
commons-collection.jar // Jakarta commons collection.
commons-dbcp.jar // Jakarta commons DBCP, providing a JDBC connection pool to web applications.
commons-el.jar // Jakarta commons el, implementing the expression language used by Jasper.
commons-pool.jar // Jakarta commons pool.
jasper-compiler.jar // The JSP 2.0 compiler.
jasper-runtime.jar // The JSP 2.0 runtime.
jsp-api.jar // The JSP 2.0 API.
naming-common.jar // The JNDI implementation used by Tomcat 5 to represent in-memory naming contexts.
naming-factory.jar // The JNDI implementation used by Tomcat 5 to resolve references to enterprise resources (EJB, connection pools).
naming-resources.jar // The specialized JNDI naming context implementation used to represent the static resources of a web application.
servlet-api.jar // The Servlet and JSP API classes.
xerces.jar // The XML parser that is visible by default to Tomcat internal classes and to web applications.
xml-apis.jar
4)Catalina ClassLoader:
用来加载实现Tomcat自己需要的类。由它加载的类对web应用都是不可见的。$CATALINA_HOME/server/classes,$CATALINA_HOME/server/lib,都由这个加载器加载
5)Shared ClassLoader:
被所有的web应用共享的类和资源由这个加载器加载。$CATALINA_BASE/shared/classed,$CATALINA_BASE/shared/lib,都由这个加载器加载
6)WebappXClassLoader:
对每个Tomcat里的web应用都创建一个加载器,web应用下的WEB-INF/classes,WEB-INF/lib,都由这个加载器加载,由它所加载的类对其他的web应用是不可见的
2 WebSphere Application Server中的类加载机制
|
Websphere Extension classloader
|
Websphere lib/app classloader
|
Websphere server classloader
/ ... ... \
2)WebSphere Extensions Classloader层
3)WebSphere lib/app Class loader
•5) Application Class loader
1)不要把应用程序依赖的资源放在JVM的系统lib下让JVM的classloader去加载,原因是如果应用程序的委托策略设成PARENT_LAST,可能会造ClassNotFoundException
2)不要修改ws.ext.dirs这个系统属性以此来增加WebSphere Extensions Classloader的加载能力,原因是被扩展的目录可能跟当前已经目录中有冲突,会导致一此不可预期的问题
3)如查开启了动态重加载,这种情况下应用如果有对本地类库的依赖关系,最好使用WebSphere "server" Class loader来加载,这样服务器的整个生命周期这些库都能被应用程序可见,即使应用程序被重新加载
二 OSGI类加载机制
5.eclipse插件加载
创建基于 Eclipse 的应用程序所需的最小插件集称为 Eclipse Rich Client Platform(RCP)。但是,插件本身不能启动。它们需要在一个环境中启动和操作。Eclipse 使用 OSGi R4 规范的实现提供了该环境。
---------------------- --------------------
|plugin A | |plugin B |
| --------------- | contibute | -------------- |
| | ext point p | <--------------- | extension | |
| --------------- |_ | -------------- |
| || | | | || |
| --------------- | |implement | -------------- |
| | interface I | <--|------------ | class C | |
| --------------- | | | -------------- |
---------------------- | ----------/ -------
| create, call |
----------------------|