亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以使用系統屬性設置 jetty 類加載語義嗎?

我可以使用系統屬性設置 jetty 類加載語義嗎?

哈士奇WWW 2023-08-16 10:02:32
在我們的項目中,我們僅使用Java API(沒有外部xml等)啟動嵌入式Jetty,然后向其部署war包。現在我遇到一種情況,ClassCastException由于從實際類路徑加載的 jar 與實際類路徑中加載的 jar 不同,所以我得到了 s .war。閱讀關于類加載的 Jetty 頁面(https://www.eclipse.org/jetty/documentation/current/jetty-classloading.html)我想看看是否可以配置WebAppClassLoader以增強被視為“系統”的類集類。有一個 Java API 可以執行此操作 ( WebAppContext.setServerClasses()),如果您使用的是 xml 配置文件,也有一種方法可以執行此操作:<Configure class="org.eclipse.jetty.webapp.WebAppContext">  <Set name="serverClasses">foo.bar.,com.acme.</Set>  ...但我想知道是否可以僅使用 Java 系統屬性來完成此操作。
查看完整描述

1 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

沒有系統屬性可用于在 WebAppContext 上配置服務器或系統類。

這是因為這種更改被認為屬于特定的 WebApp,而不是所有 WebApp。

但是,您還有另一種選擇,如果您在嵌入式碼頭中使用DeploymentManager,那么您很幸運,您可以選擇代碼。

您需要創建一個自定義項AppLifeCycle.Binding,在任何已部署的項上設置這些屬性WebAppContext(我建議綁定到deploying)。

下面是一個強制 WebAppContext 始終使用服務器/系統類加載器中的日志庫的示例。

import org.eclipse.jetty.deploy.App;

import org.eclipse.jetty.deploy.AppLifeCycle;

import org.eclipse.jetty.deploy.graph.Node;

import org.eclipse.jetty.server.handler.ContextHandler;

import org.eclipse.jetty.webapp.WebAppContext;


public class CentralizedWebAppLoggingBinding implements AppLifeCycle.Binding

{

? ? public String[] getBindingTargets()

? ? {

? ? ? ? return new String[]

? ? ? ? { "deploying" };

? ? }


? ? public void processBinding(Node node, App app) throws Exception

? ? {

? ? ? ? ContextHandler handler = app.getContextHandler();

? ? ? ? if (handler == null)

? ? ? ? {

? ? ? ? ? ? throw new NullPointerException("No Handler created for App: " + app);

? ? ? ? }


? ? ? ? if (handler instanceof WebAppContext)

? ? ? ? {

? ? ? ? ? ? WebAppContext webapp = (WebAppContext)handler;

? ? ? ? ? ? webapp.addSystemClass("org.apache.log4j.");

? ? ? ? ? ? webapp.addSystemClass("org.slf4j.");

? ? ? ? ? ? webapp.addSystemClass("org.apache.commons.logging.");

? ? ? ? }

? ? }

}

這是一個從嵌入式碼頭使用 DeploymentManager 的示例(CentralizedWebAppLoggingBinding也包含上述內容)。


ContextHandlerCollection contexts = new ContextHandlerCollection();


DeploymentManager deployer = new DeploymentManager();

if(debugIsEnabled)

{

? ? DebugListener debug = new DebugListener(System.err, true, true, true);

? ? server.addBean(debug);

? ? deployer.addLifeCycleBinding(new DebugListenerBinding(debug));

}

deployer.setContexts(contexts);

deployer.setContextAttribute(

? ? "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",

? ? ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");


WebAppProvider webAppProvider = new WebAppProvider();

webAppProvider.setMonitoredDirName(jettyBase + "/webapps");

webAppProvider.setDefaultsDescriptor(jettyHome + "/etc/webdefault.xml");

webAppProvider.setScanInterval(1);

webAppProvider.setExtractWars(true);

webAppProvider.setConfigurationManager(new PropertiesConfigurationManager());

webAppProvider.addLifeCycleListener(new CentralizedWebAppLoggingBinding());


查看完整回答
反對 回復 2023-08-16
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號