Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found 问题排查

自定义的classLoader启动spring容器,遇到
Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not subtype (classpath下有类)

Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found(classpath下没有类)
两个异常信息。

这个问题比较诡异,缺少包和没有包都有问题,有包的情况下报 not subtype(这个应该是classLoader之间的问题,直接调整类的加载策略为双亲委派策略,问题解决),没包的情况报 not found。只能走上排查之路了。
先根据堆栈进入到源码,

Caused by: javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
at com.ibatis.common.xml.NodeletParser.createDocument(NodeletParser.java:165)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:59)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:62)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:55)
at org.springframework.orm.ibatis.SqlMapClientFactoryBean.buildSqlMapClient(SqlMapClientFactoryBean.java:338)
at org.springframework.orm.ibatis.SqlMapClientFactoryBean.afterPropertiesSet(SqlMapClientFactoryBean.java:291)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585)
... 38 common frames omitted

xml的解析工具在实例化的时候出现了错误,比较奇怪的是,其他使用了相同框架的模块并不会出现这个问题。先打开日志输出的参数,在jvm的启动参数中增加 -Djaxp.debug=true

这个参数打开之后,可以输出 DocumentBuilderFactory 在实例化时会使用哪个具体的实现类去实例化。


JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: loaded from fallback value: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl using ClassLoader: null

JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null

正常的情况下,会优先使用你的配置类。你可以在 systemProperty中配置具体使用哪个类。如果没有配置,会使用 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl 的实现类,是jdk8中自带的实现类。

默认的 fallback类为:com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
注意这一小段代码,在classpath下寻找fallback类,如果没有找到,使用默认的fallback类,这个自动在包中搜索的,所以只要你的包中有相关的实现类,就会被加载并且实例化。

// Try Jar Service Provider Mechanism
T provider = findServiceProvider(type);
if (provider != null) {
return provider;
}

那问题其实已经很明确了,在classpath目录下找找是不是有相关的类,将相关的依赖包排除掉即可。

<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>

<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>

都让它使用默认的Factory类。排除掉之后,问题解决。

原文地址:https://www.cnblogs.com/zhoukedou/p/7049689.html