1

这是一个用于扫描 Servlet 3.0 初始化程序的嵌入式 Tomcat 服务器的简单方法:

public static void main(String[] args) throws ServletException, LifecycleException {
    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);

    StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File("web").getAbsolutePath());

    //declare an alternate location for your "WEB-INF/classes" dir:     
    File additionWebInfClasses = new File("target/classes");
    VirtualDirContext resources = new VirtualDirContext();
    resources.setExtraResourcePaths("/WEB-INF/classes=" + additionWebInfClasses);
    ctx.setResources(resources);

    tomcat.start();
    System.out.println("Started");
    tomcat.getServer().await();
}

我知道tomcat.util.scan.DefaultJarScanner.jarsToSkipin中的属性catalina.properties允许您限制扫描哪些 jar 以加快部署时间。我如何将同样的想法融入到嵌入式 Tomcat 代码中?

奖励:有没有办法指定要包含哪些罐子而不是要跳过哪些罐子?

4

3 回答 3

2

虽然我没有加载单个catalina.properties属性的特定方法,但我发现只需在 Tomcat 容器上下文中重载 JarScanner 本身就足够方便,如下所示:

答案在 Groovy 中(对不起,我很懒)

public static void main(String[] args) throws ServletException, LifecycleException {
    Tomcat tomcat = new TomcatWithFastJarScanner()
    tomcat.setPort(8080)

    StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File("web").getAbsolutePath())

    //declare an alternate location for your "WEB-INF/classes" dir:     
    VirtualDirContext resources = new VirtualDirContext()
    resources.setExtraResourcePaths("/WEB-INF/classes=" + new File("target/classes"))
    ctx.setResources(resources)

    tomcat.start()
    println "Started server on port 8080"
    tomcat.getServer().await()
}

private static class TomcatWithFastJarScanner extends Tomcat {
    @Override
    public void start() throws LifecycleException {
        getServer().findServices().each { service ->
            service.getContainer().findChildren().each { container ->
                container.findChildren().each { c ->
                    ((Context) c).setJarScanner(new FastJarScanner())
                }
            }
        }
        super.start()
    }
}


private static class FastJarScanner extends StandardJarScanner {
    def jarsToInclude = [ 'spring-web.*' ]

    @Override
    public void scan(ServletContext context, ClassLoader classloader,
            JarScannerCallback callback, Set<String> jarsToSkip) {
        jarsToSkip = new HashSet<String>();

        ((URLClassLoader) classloader.getParent()).getURLs().each {
            def jar = it.path.find(/[^\/]+\.jar$/)
            if(!jar) return
            for(String inclusionPattern : jarsToInclude) {
                if(jar.find(inclusionPattern))  
                    println "including jar: " + jar
                else jarsToSkip.add(jar)
            }
        }

        super.scan(context, classloader, callback, jarsToSkip);
    }
}

基本思想是我们正在查看类加载器可以看到的所有 jar,并排除所有我们不想包含的 jar。

像这样嵌入的 Tomcat 启动得非常快!

于 2013-07-12T18:45:09.070 回答
0

以下是推测性的,仅阅读规格。可以使用带有自己的 META-INF/web.xml 的 web 片段,其中 metadata-complete=true 表示没有注释扫描。

于 2013-07-12T15:01:29.933 回答
0

您可以将 Tomcat 设置 scanClassPath 扩展为 false,类似于 - 这是在用户 jkschneider 此处指出的内容之上):

new Tomcat() {
    @Override
    public void start() throws LifecycleException {
        for (final org.apache.catalina.Service service : getServer().findServices()) {
            for (final Container container : service.getContainer().findChildren()) {
                for (final Container subContainer : container.findChildren()) {
                    final StandardJarScanner jarScanner = (StandardJarScanner) ((Context) subContainer).getJarScanner();
                    jarScanner.setScanClassPath(false);
                }
            }
        }
        super.start();
    }
};

顺便说一句,更好的方法可能如下:

final Tomcat tomcat = new Tomcat();
final StandardHost tomcatHost = (StandardHost) tomcat.getHost();
        tomcatHost.setContextClass("com.fullclassname....FastStandardContext");

FastStandardContext在哪里

public class FastStandardContext extends StandardContext {
    public FastStandardContext() {
        ((StandardJarScanner) getJarScanner()).setScanClassPath(false);
    }
}

这将涵盖在运行 tomcat 后添加新 webapps 的情况。

于 2019-01-15T16:26:13.470 回答