是的,您可以拥有一个参与webapps 部署生命周期的类。
示例类:
package example.deploy.logging.config;
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 LoggingConfigBinding 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.slf4j.");
}
}
}
然后,您将拥有一段 XML,将其加载到DeploymentManager
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref id="DeploymentManager">
<Call name="addLifeCycleBinding">
<Arg>
<New class="example.deploy.logging.config.LoggingConfigBinding">
</New>
</Arg>
</Call>
</Ref>
</Configure>
有关更完整的示例,请参阅在 Jetty 上的 Logback 中筛选日志(注意:logback 是 slf4j 日志记录实现)。