javax.xml.parsers.DocumentBuilderFactory 无法在带有 Hibernate 5.2.2 升级的 Websphere Application Server 中实例化

javax.xml.parsers.DocumentBuilderFactory could not be instantiated in Websphere Application Server with Hibernate 5.2.2 Upgrade

正在将 Hibernate 4.2 升级到 Hibernate 5.2 版本

在 Websphere 8.5.5.13 版本中部署应用程序时,抛出以下错误

00000001 ContainerHelp E WSVR0501E: Error creating component com.ibm.ws.runtime.component.CompositionUnitMgrImpl@b9dc2b3a com.ibm.ws.exception.RuntimeWarning: com.ibm.ws.exception.RuntimeError: com.ibm.ws.exception.RuntimeError: java.lang.RuntimeException: javax.xml.parsers.FactoryConfigurationError: Provider javax.xml.parsers.DocumentBuilderFactory could not be instantiated: java.util.ServiceConfigurationError: javax.xml.parsers.DocumentBuilderFactory: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not a subtype at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.startCUList(CompositionUnitMgrImpl.java:1303) at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:275)

我尝试删除 xml-api jar 和 maven 排除

         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-core.version}</version>
            <exclusions>
                <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
                </exclusion>
            </exclusions>
        </dependency> 

但是,Web 应用程序部署在 Tomcat 9 中成功,没有任何更改。

堆栈跟踪:

00000001 ContainerHelp E   WSVR0501E: Error creating component com.ibm.ws.runtime.component.CompositionUnitMgrImpl@b9dc2b3a
com.ibm.ws.exception.RuntimeWarning: com.ibm.ws.exception.RuntimeError: com.ibm.ws.exception.RuntimeError: java.lang.RuntimeException: javax.xml.parsers.FactoryConfigurationError: Provider javax.xml.parsers.DocumentBuilderFactory could not be instantiated: java.util.ServiceConfigurationError: javax.xml.parsers.DocumentBuilderFactory: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not a subtype
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.startCUList(CompositionUnitMgrImpl.java:1303)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:275)
    at com.ibm.ws.runtime.component.ContainerHelper.startComponents(ContainerHelper.java:540)
    at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:627)
    at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:618)
    at com.ibm.ws.runtime.component.ApplicationServerImpl.start(ApplicationServerImpl.java:252)
    at com.ibm.ws.runtime.component.ContainerHelper.startComponents(ContainerHelper.java:540)
    at com.ibm.ws.runtime.component.ContainerImpl.startComponents(ContainerImpl.java:627)
    at com.ibm.ws.runtime.component.ContainerImpl.start(ContainerImpl.java:618)
    at com.ibm.ws.runtime.component.ServerImpl.start(ServerImpl.java:540)
    at com.ibm.ws.runtime.WsServerImpl.bootServerContainer(WsServerImpl.java:316)
    at com.ibm.ws.runtime.WsServerImpl.start(WsServerImpl.java:229)
    at com.ibm.ws.runtime.WsServerImpl.main(WsServerImpl.java:702)
    at com.ibm.ws.runtime.WsServer.main(WsServer.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at com.ibm.wsspi.bootstrap.WSLauncher.launchMain(WSLauncher.java:234)
    at com.ibm.wsspi.bootstrap.WSLauncher.main(WSLauncher.java:96)
    at com.ibm.wsspi.bootstrap.WSLauncher.run(WSLauncher.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at org.eclipse.equinox.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:587)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:198)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at org.eclipse.core.launcher.Main.invokeFramework(Main.java:340)
    at org.eclipse.core.launcher.Main.basicRun(Main.java:282)
    at org.eclipse.core.launcher.Main.run(Main.java:981)
    at com.ibm.wsspi.bootstrap.WSPreLauncher.launchEclipse(WSPreLauncher.java:407)
    at com.ibm.wsspi.bootstrap.WSPreLauncher.main(WSPreLauncher.java:170)
Caused by: com.ibm.ws.exception.RuntimeError: com.ibm.ws.exception.RuntimeError: java.lang.RuntimeException: javax.xml.parsers.FactoryConfigurationError: Provider javax.xml.parsers.DocumentBuilderFactory could not be instantiated: java.util.ServiceConfigurationError: javax.xml.parsers.DocumentBuilderFactory: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not a subtype
    at com.ibm.wsspi.runtime.component.WsComponentImpl.join(WsComponentImpl.java:436)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.startCUList(CompositionUnitMgrImpl.java:1295)
    ... 39 more
Caused by: com.ibm.ws.exception.RuntimeError: java.lang.RuntimeException: javax.xml.parsers.FactoryConfigurationError: Provider javax.xml.parsers.DocumentBuilderFactory could not be instantiated: java.util.ServiceConfigurationError: javax.xml.parsers.DocumentBuilderFactory: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not a subtype
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:818)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.run(ApplicationMgrImpl.java:2279)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:5554)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:5680)
    at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:255)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:2284)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:436)
    at com.ibm.ws.runtime.component.CompositionUnitImpl.start(CompositionUnitImpl.java:123)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:379)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.access0(CompositionUnitMgrImpl.java:127)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$CUInitializer.run(CompositionUnitMgrImpl.java:985)
    at com.ibm.wsspi.runtime.component.WsComponentImpl$_AsynchInitializer.run(WsComponentImpl.java:524)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
Caused by: java.lang.RuntimeException: javax.xml.parsers.FactoryConfigurationError: Provider javax.xml.parsers.DocumentBuilderFactory could not be instantiated: java.util.ServiceConfigurationError: javax.xml.parsers.DocumentBuilderFactory: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not a subtype
    at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:1063)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:795)
    ... 12 more
Caused by: javax.xml.parsers.FactoryConfigurationError: Provider javax.xml.parsers.DocumentBuilderFactory could not be instantiated: java.util.ServiceConfigurationError: javax.xml.parsers.DocumentBuilderFactory: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not a subtype
    at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
    at com.ibm.ws.webservices.engine.utils.XMLUtils.getDocumentBuilderFactory(XMLUtils.java:460)
    at com.ibm.ws.webservices.engine.utils.XMLUtils.newDocument(XMLUtils.java:539)
    at com.ibm.ws.webservices.engine.utils.XMLUtils.newDocument(XMLUtils.java:557)
    at com.ibm.ws.webservices.engine.encoding.custom.Utils.loadCustomProviders(Utils.java:267)
    at com.ibm.ws.webservices.tools.resource.ToolEnv.run(ToolEnv.java:332)
    at java.security.AccessController.doPrivileged(AccessController.java:638)
    at com.ibm.ws.webservices.tools.resource.ToolEnv.loadCustomProviders(ToolEnv.java:329)
    at com.ibm.ws.webservices.tools.resource.ToolEnv.getCustomBindingProviders(ToolEnv.java:324)
    at com.ibm.ws.webservices.wsdl.toJava.Emitter.initCustomBindingRegistry(Emitter.java:972)
    at com.ibm.ws.webservices.wsdl.toJava.Emitter.setup(Emitter.java:959)
    at com.ibm.ws.webservices.wsdl.Parser.parse(Parser.java:326)
    at com.ibm.ws.webservices.wsdl.Parser.run(Parser.java:313)
    at com.ibm.ws.webservices.tools.WSDLQuery.parse(WSDLQuery.java:142)
    at com.ibm.ws.webservices.engine.deployment.wsdd.WSDDGen.getWSDLQuery(WSDDGen.java:214)
    at com.ibm.ws.webservices.engine.deployment.wsdd.WSDDPort.expand(WSDDPort.java:554)
    at com.ibm.ws.webservices.engine.deployment.wsdd.WSDDPort._initTMR(WSDDPort.java:311)
    at com.ibm.ws.webservices.component.WSServerImpl.setupWsddPort(WSServerImpl.java:1264)
    at com.ibm.ws.webservices.component.WSServerImpl.warMetaDataCreated(WSServerImpl.java:2151)
    at com.ibm.ws.webservices.component.WSServerImpl.metaDataCreated(WSServerImpl.java:639)
    at com.ibm.ws.runtime.component.MetaDataMgrImpl.fireMetaDataCreated(MetaDataMgrImpl.java:279)
    at com.ibm.ws.runtime.component.MetaDataMgrImpl.fireMetaDataCreated(MetaDataMgrImpl.java:262)
    at com.ibm.ws.webcontainer.metadata.WebMetaDataFactory.createMetaData(WebMetaDataFactory.java:244)
    at com.ibm.ws.runtime.component.MetaDataMgrImpl.createMetaDataFromFactories(MetaDataMgrImpl.java:228)
    at com.ibm.ws.runtime.component.MetaDataMgrImpl.createMetaData(MetaDataMgrImpl.java:411)
    at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:631)
    at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:979)

看起来服务器级加载程序和 "parent last" 应用程序 class 加载程序之间存在交叉链接。服务器级别(实际上,更可能是 Java 级别)加载程序加载 DocumentBuilderFactory API class,然后它会转到线程上下文 class 加载程序以查找其实现.线程上下文 class 加载器是父级最后一个应用程序加载器,它不会立即委托给它的父级,因此它会在那里找到实现,然后链接到应用程序的 API class。原来的APIclass(来自Java)和"new"APIclass(来自app)不能相互转换,因为它们来自不同的 class 个加载器,操作失败。

就实际解决这个问题而言:

1) 有 99.9% 的机会你不需要带上你自己的 JAXP 版本,它已经包含在 JDK 很长一段时间了(看起来你正在打包Xerces,IBM Java 中包含的版本。删除它将消除该异常。沿着类似的路线,您可以通过切换到 "parent first" class 加载程序委派来解决这个问题 - 如果您不需要用自己的服务器覆盖 APIs,它会大大降低出现问题的风险像这样。

2) 那就是说...您不必一定要删除它,因为服务器通常允许应用程序自带解析器。该异常堆栈意味着某些 WebSphere 代码在创建 XML 对象时可能无法正确保护其线程上下文,您可能希望打开支持票并让 IBM 验证此处是否存在实际错误。