使用Jetty加载WAR文件时忽略web.xml [英] Ignoring web.xml when loading a WAR file with Jetty

查看:119
本文介绍了使用Jetty加载WAR文件时忽略web.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jetty一个可自行执行的WAR软件包.默认情况下,它使用web.xml进行配置.如果给出了运行时选项,我想通过ServletContextHandler#addServlet,#addEventListener和...的Java代码级配置覆盖web.xml.

I'm trying a self-executable WAR package with Jetty. It configures with web.xml by default. If a run-time option is given, I wanted to override web.xml by Java code-level configuration with ServletContextHandler#addServlet, #addEventListener, and ...

加载WAR软件包时可以忽略web.xml吗?

Can I ignore web.xml while loading a WAR package?

% java -jar foobar.jar  # Use web.xml
% java -jar foobar.jar --customize=something  # Use Java code to configure


// Example

WebAppContext webapp = new WebAppContext();
webapp.setWar(warLocation.toExternalForm());
webapp.setContextPath("/");
if ( /* has run-time options */ ) {
  webapp.setWar(warLocation.toExternalForm()); // But, no load web.xml!
  // Emulates web.xml.
  webapp.addEventListener(...);
  webapp.setInitParameter("resteasy.role.based.security", "true");
  webapp.addFilter(...);
} else {
  webapp.setWar(warLocation.toExternalForm()); // Loading web.xml.
}


其他问题:

在调用server.start()之前,不会加载WEB-INF/下的类.我可以对WEB-INF/下的某些类进行一些配置webapp.something()吗? (例如,扩展WebInfConfiguration还是进行类似WebInfConfiguration的类加载?)


Additional Question:

Before server.start() is called, classes under WEB-INF/ are not loaded. Can I do some configuration webapp.something() with some classes under WEB-INF/? (E.g. extend WebInfConfiguration or do a similar class-loading that WebInfConfiguration does?)

例如,我想做类似的事情:

For example, I'd like to do something like:

  • webapp.addEventListener(new SomeClassUnderWebInf()));
  • webapp.addEventListener(someInjector.inject(SomeClassUnderWebInf.class));

在server.start()之前.

before server.start().

推荐答案

亲自处理WebAppContext配置.

Handle the WebAppContext Configuration yourself.

例如:

private static class SelfConfiguration extends AbstractConfiguration
{
    @Override
    public void configure(WebAppContext context) throws Exception
    {
        // Emulates web.xml.
        webapp.addEventListener(...);
        webapp.setInitParameter("resteasy.role.based.security", "true");
        webapp.addFilter(...);
    }
}

public static void main(String[] args) throws Exception
{
    Server server = new Server(8080);

    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    if (useWebXml)
    {
        webapp.setConfigurationClasses(WebAppContext.getDefaultConfigurationClasses());
    } 
    else 
    {
        webapp.setConfigurations(new Configuration[] { 
            new SelfConfiguration() 
        });
    }
    webapp.setWar("path/to/my/test.war");
    webapp.setParentLoaderPriority(true);
    server.setHandler(webapp);
    server.start();
    server.join();
}

这篇关于使用Jetty加载WAR文件时忽略web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆