springboot啓動嵌入式tomcat報錯找不到jar包,關鍵字:FileNotFoundException,derbyLocale_cs.jar,StandardJarScanner.scan

異常:java

java.io.FileNotFoundException: /Users/lanhuajian/.m2/repository/org/apache/derby/derby/10.13.1.1/derbyLocale_cs.jar (No such file or directory)
	at java.util.zip.ZipFile.open(Native Method) ~[?:1.8.0_152]
	at java.util.zip.ZipFile.<init>(ZipFile.java:225) ~[?:1.8.0_152]
	at java.util.zip.ZipFile.<init>(ZipFile.java:155) ~[?:1.8.0_152]
	at java.util.jar.JarFile.<init>(JarFile.java:166) ~[?:1.8.0_152]
	at java.util.jar.JarFile.<init>(JarFile.java:130) ~[?:1.8.0_152]
	at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:60) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
	at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
	at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:338) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
	at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:288) [tomcat-embed-core-8.5.23.jar:8.5.23]
	at org.apache.jasper.servlet.TldScanner.scanJars(TldScanner.java:262) [tomcat-embed-jasper-8.5.23.jar:?]
	at org.apache.jasper.servlet.TldScanner.scan(TldScanner.java:104) [tomcat-embed-jasper-8.5.23.jar:?]
	at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:101) [tomcat-embed-jasper-8.5.23.jar:?]
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196) [tomcat-embed-core-8.5.23.jar:8.5.23]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.23.jar:8.5.23]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) [tomcat-embed-core-8.5.23.jar:8.5.23]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [tomcat-embed-core-8.5.23.jar:8.5.23]
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [?:1.8.0_152]
	at java.util.concurrent.FutureTask.run(FutureTask.java) [?:1.8.0_152]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_152]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_152]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_152]

springboot集成hadoop等組件後會報這個錯,通常和hadoop集成須要把老版本的sevlet、jsp、tomcat依賴去掉,可是我去掉以後仍是存在。 因此我跟蹤了springboot初始化tomcat的代碼,發如今TomcatEmbeddedServletContainerFactory#prepareContext()有個tldSkipPatterns的東西, 就心想這個是否是會告訴tomcat不要掃描這些jar包呢?試了一下,真的能夠!spring

所以能夠得出以下解決方案,在初始化TomcatEmbeddedServletContainerFactory後,在後置bean處理器中給它加上tldSkipPatterns便可apache

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

@Configuration
public class TomcatContainerConfig {
	@Value("${tldSkipPatterns}")
	private String[] tldSkipPatterns;

	@Bean
	public BeanPostProcessor TomcatContainerPostProcessor() {
		List<String> notEmptyTldSkipPatterns = Arrays.stream(tldSkipPatterns)
			.filter(tldSkipPattern -> !tldSkipPattern.trim().isEmpty())
			.collect(toList());


		return new BeanPostProcessor() {
			@Override
			public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
				return bean;
			}

			@Override
			public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
				if (beanName.equals("tomcatEmbeddedServletContainerFactory") &&
						bean instanceof TomcatEmbeddedServletContainerFactory) {
					TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) bean;
					if (!notEmptyTldSkipPatterns.isEmpty()) {
						factory.addTldSkipPatterns(notEmptyTldSkipPatterns.toArray(new String[0]));
					}
				}
				return bean;
			}
		};
	};
}

增長下面的配置到 application.properties:api

tldSkipPatterns=derbyLocale_*.jar,jaxb-api.jar,jsr173_1.0_api.jar,jaxb1-impl.jar,activation.jar
相關文章
相關標籤/搜索