每当我在 tomcat 中部署 Web 应用程序时,WEB-INF/lib 中的所有 jar 都会加载到应用程序 ClassLoader 中。
我很少有其他位置有一些罐子,如WEB-INF/ChildApp1/* .jar,WEB-INF/ChildApp2/*.jar.....根据用户请求,我想将一组jar加载到当前的类加载器中。
注意:我不想创建任何子类加载器。
我真正的要求是,以编程方式如何将 jar 添加到当前的类加载器中。
我做过一次,但这有点黑客。请参阅下面的代码:
final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
final Class<URLClassLoader> sysclass = URLClassLoader.class;
// TODO some kind of a hack. Need to invent better solution.
final Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
for (final File jar : jars) {
method.invoke(sysloader, new URL[] { jar.toURI().toURL() });
}
您需要将 ClassLoader.getSystemClassloader(( 更改为要使用的类加载器。您还必须检查这是否是URLClassloader的实例我认为有更好的解决方案,但这对我有用
实现自己的WebappClassLoaderBase
,以便在context.xml
的loader
配置中定义。
实施您的WebappClassLoaderBase
最简单的方法是将WebappClassLoader
扩展为下一个
package my.package;
public class MyWebappClassLoader extends WebappClassLoader {
public MyWebappClassLoader() {
}
public MyWebappClassLoader(final ClassLoader parent) {
super(parent);
}
@Override
public void start() throws LifecycleException {
String[] paths = {"/WEB-INF/ChildApp1/lib", "/WEB-INF/ChildApp2/lib"};
// Iterate over all the non standard locations
for (String path : paths) {
// Get all the resources in the current location
WebResource[] jars = resources.listResources(path);
for (WebResource jar : jars) {
// Check if the resource is a jar file
if (jar.getName().endsWith(".jar") && jar.isFile() && jar.canRead()) {
// Add the jar file to the list of URL defined in the parent class
addURL(jar.getURL());
}
}
}
// Call start on the parent class
super.start();
}
}
部署您的WebappClassLoaderBase
- 使用与此处提供的雄猫版本相对应的雄猫罐构建您自己的
WebappClassLoaderBase
。 - 从中创建罐 子
- 并将罐子放入 tomcat/lib 中,以便从
Common ClassLoader
中获取
配置您的WebappClassLoaderBase
在context.xml
中定义WebappClassLoaderBase
<Context>
...
<Loader loaderClass="my.package.MyWebappClassLoader" />
</Context>
完成后,现在您的网络应用程序将能够从/WEB-INF/ChildApp1/lib
和/WEB-INF/ChildApp2/lib
加载jar文件。
响应更新
由于您想做同样的事情,但只有一个 war
,您将需要使用黑客来动态添加您的jar
文件。
以下是您可以继续操作的方法:
实现ServletContextListener
以添加 jar 文件
为了在初始化上下文时动态添加jar
文件,您需要创建一个ServletContextListener
,该将通过反射调用URLClassLoader#addURL(URL)
,这是一个丑陋的黑客,但它有效。请注意,它之所以有效,是因为 Tomcat 中 Web 应用程序的ClassLoader
是一个WebappClassLoader
,实际上是 URLClassLoader
的子类。
package my.package;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(final ServletContextEvent sce) {
try {
// Get the method URLClassLoader#addURL(URL)
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
// Make it accessible as the method is protected
method.setAccessible(true);
String[] paths = {"/WEB-INF/ChildApp1/lib", "/WEB-INF/ChildApp2/lib"};
for (String path : paths) {
File parent = new File(sce.getServletContext().getRealPath(path));
File[] jars = parent.listFiles(
new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return name.endsWith(".jar");
}
}
);
if (jars == null)
continue;
for (File jar : jars) {
// Add the URL to the context CL which is a URLClassLoader
// in case of Tomcat
method.invoke(
sce.getServletContext().getClassLoader(),
jar.toURI().toURL()
);
}
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Override
public void contextDestroyed(final ServletContextEvent sce) {
}
}
申报您的ServletContextListener
在 Web 应用程序的web.xml
中,只需添加:
<listener>
<listener-class>my.package.MyServletContextListener</listener-class>
</listener>