带有Servlet 3.0的嵌入式Tomcat-如何在扫描时跳过某些jar?
这是嵌入式Tomcat服务器扫描Servlet 3.0初始化程序的简单方法:
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.jarsToSkip
中catalina.properties
允许你这瓶被扫描加快部署时间限制。我如何将相同的想法整合到嵌入式Tomcat代码中?
奖励:有没有一种方法可以指定要 包括 的jar而不是要 跳过的 jar ?
回答:
尽管我没有特定的方式来加载单个catalina.properties
属性,但我发现它很方便,只需将JarScanner本身重载在Tomcat容器上下文中,如下所示:
(对不起,我很懒)
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嵌入式可以像这样快速启动!
以上是 带有Servlet 3.0的嵌入式Tomcat-如何在扫描时跳过某些jar? 的全部内容, 来源链接: utcz.com/qa/422669.html