Java:Spring: XML conflict on reading spring contex

2020-06-19 03:26发布

Any advise or help is much appreciated.

There are many reference links, but there is no clear solution. I wonder how Spring projects interact with PLSQL procedures that returns XMLType output. you would need the XMLParser, and then we all will get this error, when used with Spring. Does anyone has any recommendations?

I have a Maven spring app(mvc/Rest) which is running good, and I added the below dependencies for interacting with the PLSQL procedures,

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>xdb6</artifactId>
        <version>11.2.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.xmlparserv2</groupId>
        <artifactId>xmlparserv2</artifactId>
        <version>11.1.1</version>
    </dependency>

After including xdb6 and xmlparserv2, I get the below error on Spring application startup(while running the server)

2014-08-19 18:28:51,898 INFO | main | o.s.beans.factory.xml.XmlBeanDefinitionReader

Loading XML bean definitions from class path resource [META-INF/spring/myapp-context.xml] 


<Line 43, Column 57>: XML-24509: (Error) Duplicated definition for: 'identifiedType'
<Line 60, Column 28>: XML-24509: (Error) Duplicated definition for: 'beans'
<Line 140, Column 34>: XML-24509: (Error) Duplicated definition for: 'description'
<Line 152, Column 29>: XML-24509: (Error) Duplicated definition for: 'import'
<Line 174, Column 28>: XML-24509: (Error) Duplicated definition for: 'alias'
<Line 203, Column 33>: XML-24509: (Error) Duplicated definition for: 'beanElements'
<Line 218, Column 44>: XML-24509: (Error) Duplicated definition for: 'beanAttributes'
<Line 462, Column 43>: XML-24509: (Error) Duplicated definition for: 'meta'
<Line 470, Column 35>: XML-24509: (Error) Duplicated definition for: 'metaType'
<Line 487, Column 27>: XML-24509: (Error) Duplicated definition for: 'bean'
<Line 507, Column 38>: XML-24509: (Error) Duplicated definition for: 'constructor-arg'
.....

2014-08-19 18:28:52,014 ERROR | main | org.springframework.web.servlet.DispatcherServlet       | Context initialization failed 
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 16 in XML document from class path resource [META-INF/spring/myapp-context.xml] is invalid; nested exception is oracle.xml.parser.schema.XSDException: Duplicated definition for: 'identifiedType'
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]

Line 16 in XML document from class path resource [META-INF/spring/myapp-context.xml] is invalid;

myapp-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.1.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd">

Line16 is http://www.springframework.org/schema/cache/spring-cache.xsd">

Suppose I remove Line16 and its references, then the same error is thrown at a different line, say Line 13

More error stack trace:

Caused by: org.xml.sax.SAXParseException: <Line 13, Column 70>: XML-24500: (Error) Can not build schema 'http://www.springframework.org/schema/tx' located at 'http://www.springframework.org/schema/tx/spring-tx.xsd'
    at oracle.xml.parser.v2.XMLError.flushErrorHandler(XMLError.java:425) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.v2.XMLError.flushErrors1(XMLError.java:287) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:343) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:226) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.jaxp.JXDocumentBuilder.parse(JXDocumentBuilder.java:155) ~[xmlparserv2-11.1.1.jar:na]


Caused by: oracle.xml.parser.schema.XSDException: Duplicated definition for: 'identifiedType'
    at oracle.xml.parser.schema.XSDBuilder.buildSchema(XSDBuilder.java:794) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.schema.XSDBuilder.build(XSDBuilder.java:489) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.schema.XSDValidator.processSchemaLocation(XSDValidator.java:999) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.schema.XSDValidator.startElement(XSDValidator.java:604) ~[xmlparserv2-11.1.1.jar:na]

Update 1

Based on the posted answer, it says a quick solution is available,

To override the changes made by xmlparserv2.jar, add the following JVM properties to your application server startup arguments. The java.xml.parsers.FactoryFinder logic will check environment variables first.

-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

 -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl

 -Djavax.xml.transform.TransformerFactory=com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

This is not recommended, as clearly stated in the link https://community.oracle.com/thread/1080787

Another solution is mentioned here (related to hibernate, but similar): Using Oracle XMLType column in hibernate

In a maven spring project, override the xmlparserv2.jar settings by creating the following files in the %PROJECT_HOME%/src/main/resources directory:

%PROJECT_HOME%/src/main/resources/META-INF/services/javax.xml.parsers.DocumentBuilderFactory (which defines com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl as the default)
%PROJECT_HOME%/src/main/resources/META-INF/services/javax.xml.parsers.SAXParserFactory (which defines com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl as the default)
%PROJECT_HOME%/src/main/resources/META-INF/services/javax.xml.transform.TransformerFactory (which defines com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl as the default)

This is not clear to me.

How to drop these files to the mentioned directory? and is that a recommended solution?

Update 2

I tried adding the VM argument -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl and it works.

However looking for an alternate, and more appropriate solution. This is just a workaround. When the application is deployed to server via say Jenkins, its not proper to set these VM argument settings related to a XML parser.

Update 3

@Ian Roberts's answer fixed the issue. I cleaned up the context.xml to use version number for the xsd consistently.

3条回答
倾城 Initia
2楼-- · 2020-06-19 04:15

This post is quite old, but anyway I ran across this today and found the cause and a different solution. It turns out that when you bring the oracle jdbc driver dependency via maven using Oracle's maven repo, it transitively gets other libraries which causes this problem when parsing the XMLs. I just excluded the transitive dependencies for the oracle driver and everything works just fine. I don't think those extra libs are necessary at all at runtime.

If you have this in your pom:

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
</dependency>

This is the dependency tree:

  • ojdbc7 : 12.1.0.2
    • xdb6 : 12.1.0.2
    • orai18n : 12.1.0.2
    • xmlparserv2 : 12.1.0.2 <-- Culprit
    • oraclepki : 12.1.0.2
    • osdt_cert : 12.1.0.2
    • osdt_core : 12.1.0.2

So you can exclude the offending library like this:

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
    <exclusions>
        <exclusion>
            <artifactId>xmlparserv2</artifactId>
            <groupId>com.oracle.jdbc</groupId>
        </exclusion>
    </exclusions>
</dependency>

I actually excluded all of the transitive dependencies, and everything seems to work just fine. I'm not sure if they are actually used. I don't think so because the manual way of including the jdbc library is by simply downloading the ojdbc jar.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-06-19 04:17

Here there is your solution.

-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl`
查看更多
虎瘦雄心在
4楼-- · 2020-06-19 04:21

Your schemaLocation is a mixture of versioned (spring-beans-3.2.xsd) and unversioned (spring-tx.xsd) schema names. You should use one style or the other consistently rather than mixing the two. I suspect what's happening here is that you're explicitly referring to version 3.2 of spring-beans but one of your unversioned schemas imports a different version of the same schema, resulting in two conflicting definitions of beans:identifiedType.

查看更多
登录 后发表回答