Skip to content

Instantly share code, notes, and snippets.

@yidongnan
Last active August 29, 2015 14:05
Show Gist options
  • Save yidongnan/1a97b90115332cfbf080 to your computer and use it in GitHub Desktop.
Save yidongnan/1a97b90115332cfbf080 to your computer and use it in GitHub Desktop.
自定义Tomcat的ClassLoader
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Loader className="TomcatClassLoader" />
</Context>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ClassLoaderTest</groupId>
<artifactId>ClassLoaderTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.52</version>
</dependency>
</dependencies>
</project>
/**
* User: Michael Chen
* Email: yidongnan@gmail.com
* Date: 2014/8/12
* Time: 16:33
*/
public class TestClassLoader extends org.apache.catalina.loader.WebappClassLoader {
public TestClassLoader(ClassLoader parent) {
this.setParentClassLoader(parent);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
System.out.println("["+name+"]");
for (int i = 0; i < stackTraceElements.length; i++) {
if (i > 20) continue;
if (i != 0) System.out.print("\t");
StackTraceElement stackTraceElement = stackTraceElements[i];
System.out.println(stackTraceElement.getClassName() + "(" + stackTraceElement.getFileName() + ":" +
stackTraceElement.getLineNumber() + ")");
}
return super.loadClass(name);
} catch (ClassNotFoundException e) {
System.err.println(name + " no found");
throw e;
}
}
@Override
public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
try {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
System.out.println("["+name+"]");
for (int i = 0; i < stackTraceElements.length; i++) {
if (i > 20) continue;
if (i != 0) System.out.print("\t");
StackTraceElement stackTraceElement = stackTraceElements[i];
System.out.println(stackTraceElement.getClassName() + "(" + stackTraceElement.getFileName() + ":" +
stackTraceElement.getLineNumber() + ")");
}
return super.loadClass(name, resolve);
} catch (ClassNotFoundException e) {
System.err.println(name + " no found");
throw e;
}
}
}
import org.apache.catalina.loader.WebappLoader;
/**
* User: Michael Chen
* Email: yidongnan@gmail.com
* Date: 2014/8/12
* Time: 16:33
*/
public class TomcatClassLoader extends WebappLoader {
@Override
public ClassLoader getClassLoader() {
setLoaderClass("TestClassLoader");
return super.getClassLoader();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment