知识库--The Loader Interface(47)

来源:互联网 发布:淘宝客服对话案例 编辑:程序博客网 时间:2024/06/04 18:26

The Loader Interface

//servlet使用的 类和库 受限
There are rules in loading servlet and other classes in a web application. For example, a servlet in an application can use classes deployed to the WEB-INF/classes directory and any sub-directory under it. However, servlets do not have access to other classes, even though those classes are included in the CLASSPATH of the JVM running Tomcat. Also,* a servlet can only access libraries deployed under the WEB-INF/lib directory and not other directories*.

A Tomcat loader represents a web application loader rather than a class loader. A loader must implements the org.apache.catalina.Loader interface. The loader implementation uses a custom class loader represented by the org.apche.catalina.loader.WebappClassLoader class. You can obtain the ClassLoader inside a web loader using the Loader interface’s getClassLoader method.

Among others, the Loader interface defines methods to work with a collection of repositories. The WEB-INF/classes and WEB-INF/lib of a web application are directories to be added as repositories. The Loader interface’s addRepository method is used to add a repository and its findRepositories method returns an array of all repositories.

A Tomcat loader implementation is usually associated with a context , and the getContainer and setContainer methods of the Loader interface are used for builfing this association. A loader can also supports reloading, if one or more classes in a context have been modified. This way, a servlet programmer can recompile a servlet or a supporting class and the new class will be reloaded without restarting Tomcat. For the reloading purpose, the Loader interface has the modified method. In a loader implementation, the modified method must return true if one or more classes in its repositories have been modified, and therefore reloading is required. A loader does not do the reloading itself, however. Instead, it calls the Context interface’s reload method. Two other methods, setReloadable and getReloadable, are used to determine if reloading is enabled in the Loader. By default, in the standard implementation of Context, reloading is not enabled. Therefore, to enable reloading of a context, you need to add a Context element for that context in your server.xml file, such as the following:

    <Context path="" docBase="" debug="0" reloadable="true"/>

//是否走委托模式
Also, a Loader implementation can be told whether or not to delegate to a parent class loader. For this purpose, the Loader interface provides the getDelegate and setDelegate methods.

    public interface Loader{        public ClassLoader getClassLoader();        public Container getContainer();        public void setContainer(Container container);        public DefaultContext getDefaultContext();        public void setDefaultContext(DefaultContext defaultContext);        public boolean getDelegate();        public void setDelegate(boolean delegate);        public String getInfo();        public boolean getReloadable();        public void setReloadable(boolean reloadable);        public void addPropertyChangeListener(PropertyChangeListener listener);        public void addRepository(String repository);        public String[] findRepositories();        public boolean modified();        public void removePropertyChangeListener(PropertyChangeListener listener);    }

Catalina provides the org.apache.catalina.loader.WebappLoader as an implementation of the Loader interface. For its class loader,* the WebappLoader object contains an instance of the org.apache.catalina.loader.WebappClassLoader class*, which extends the java.net.URLClassLoader class.

Note whenever the container associated with a loader needs a servlet class,i.e. when its invoke method is called, the container first calls the loader’s getClassLoader method to obtain the class loader. The container then calls the loadClass method of the class loader to load the servlet class. to see “StandardWrapper”.

这里写图片描述

0 0
原创粉丝点击