猿问

我可以使用系统属性设置 jetty 类加载语义吗?

在我们的项目中,我们仅使用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 系统属性来完成此操作。


哈士奇WWW
浏览 109回答 1
1回答

慕斯王

没有系统属性可用于在 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());
随时随地看视频慕课网APP

相关分类

Java
我要回答