gpt4 book ai didi

java - 在不发布的情况下提供模块时的 Tomcat 类加载顺序

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:37:37 26 4
gpt4 key购买 nike

我正在使用 tomcat 和 eclipse。

根据tomcat的文档:

从 Web 应用程序的角度来看,类或资源加载按以下顺序查看以下存储库:

  • JVM 系统类加载器类的引导类(描述以上)
  • /WEB-INF/web 应用程序的类
  • /WEB-INF/lib/*.jar 你的网络应用程序
  • 通用类加载器类(如上所述)

所以当加载类时,tomcat 会先查找WEB-INF/classes,然后再查找WEB-INF/lib。并且我们可以覆盖WEB-INF/lib中jar文件中的一些类,tomcat会拾取被覆盖的那个。

但是现在如果我通过选中“Serve modules with out publishing”来更改 tomcat 服务器选项,那么被覆盖的类将不再被加载。

是否有任何解决方案可以让它再次工作,但我仍然希望 tomcat 无需发布即可提供模块服务。


编辑:
我发现一些东西可能有用,在这个文件夹中 f:\eclipse_projects\.metadata.plugins\org.eclipse.wst.server.core\tmp0\conf有一个 server.xml 它包含这样的内容:

<Resources className="org.eclipse.jst.server.tomcat.loader.WtpDirContext" 
extraResourcePaths=""
virtualClasspath="F:\eclipse_projects\ALS7C3\bin"/>
<Loader className="org.eclipse.jst.server.tomcat.loader.WtpWebappLoader"
useSystemClassLoaderAsParent="false"
virtualClasspath="F:\eclipse_projects\ALS7C3\bin"/>

似乎在运行 tomcat 并选中“Serve modules without publishing”选项时,eclipse 将使用它自己的加载程序。此加载程序包含在一个 jar 文件中,该文件将被复制到 f:\eclipse_projects\.metadata。 plugins\org.eclipse.wst.server.core\tmp0\lib** 当你在 eclipse 中启动 tomcat 时。这是 **org.eclipse.jst.server.tomcat.loader.WtpDirContext 的部分源代码:

public Object lookup(String name) throws NamingException {
if (name.startsWith("/WEB-INF/") && name.endsWith(".tld")) {
String tldName = name.substring(name.lastIndexOf("/") + 1);
if (virtualMappings != null && virtualMappings.containsKey(tldName)) {
return new FileResource(virtualMappings.get(tldName));
}
} else if (tagfileMappings != null && name.startsWith("/META-INF/tags")
&& (name.endsWith(".tag") || name.endsWith(".tagx"))) {
// already loaded tag file
return new FileResource(tagfileMappings.get(name));
}

Object retSuper;
NamingException superEx;
try {
retSuper = super.lookup(name);
return retSuper;
} catch (NamingException ex) {
retSuper = null;
superEx = ex;
}
if (mappedResourcePaths != null) {
// Perform lookup using the extra resource paths
for (Map.Entry<String, List<String>> mapping : mappedResourcePaths.entrySet()) {
String path = mapping.getKey();
List<String> dirList = mapping.getValue();
if (name.equals(path)) {
for (String resourcesDir : dirList) {
File f = new File(resourcesDir);
if (f.exists() && f.canRead()) {
if (f.isFile()) {
return new FileResource(f);
}
else {
// TODO Handle directory
}
}
}
}
path += "/";
if (name.startsWith(path)) {
String res = name.substring(path.length());
for (String resourcesDir : dirList) {
File f = new File (resourcesDir + "/" + res);
if (f.exists() && f.canRead()) {
if (f.isFile()) {
return new FileResource(f);
}
else {
// TODO Handle directory
}
}
}
}
}
}
throw superEx;
}

好像是先处理jsp标签库再调用super.lookup,如果在super.lookup中找不到,会尝试加载资源在 virtualClasspath 中,在我的示例中为“F:\eclipse_projects\ALS7C3\bin”,它是在不发布的情况下提供模块时 eclipse 输出类文件的位置。

所以,我想,如果我可以覆盖 org.eclipse.jst.server.tomcat.loader.WtpDirContextlookup 方法,我就能得到我想要的东西,但是这个 jar 文件包含在 org.eclipse.jst.server.tomcat.core.jar 中,它们都已签名。

我不知道如何覆盖这样的 jar 文件。

有人可以帮忙吗?

最佳答案

以下是我使用 eclipse/tomcat 进行热部署的方法:

  1. 停止tomcat
  2. 打开tomcat配置菜单
  3. 在“服务器选项”选项卡中选中“不发布服务模块”
  4. 在“发布”选项卡中选中“从不自动发布”
  5. 保存配置
  6. 以 Debug模式启动tomcat(这很重要)

如果您更改了配置文件,也许可以尝试使用新的 tomcat。

关于java - 在不发布的情况下提供模块时的 Tomcat 类加载顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19675931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com