springboot启动嵌入式tomcat报错找不到jar包,关键字:FileNotFoundException,derbyLocale_cs.jar,StandardJarScanner.scan...

异常:

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包呢?试了一下,真的可以!

因此可以得出如下解决方案,在初始化TomcatEmbeddedServletContainerFactory后,在后置bean处理器中给它加上tldSkipPatterns即可

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:

tldSkipPatterns=derbyLocale_*.jar,jaxb-api.jar,jsr173_1.0_api.jar,jaxb1-impl.jar,activation.jar

转载于:https://www.cnblogs.com/lanhj/p/8484211.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值