Spring批处理xmlns架构错误

Posted

技术标签:

【中文标题】Spring批处理xmlns架构错误【英文标题】:Spring batch xmlns schema error 【发布时间】:2012-12-13 14:08:29 【问题描述】:

我们已经使用 eclipse 构建了一个 spring-batch 应用程序。每当我通过eclipse执行程序时,它运行良好。但是当我尝试生成并运行使用 ant 创建的 jar 文件时,我得到了这个丑陋的堆栈跟踪。

2012 年 12 月 27 日 11:10:30,880 1141 [主][] 错误 (CommandLineJobRunner.java:355):作业终止错误:第 12 行 来自类路径资源 [launch-context.xml] 的 XML 文档无效; 嵌套异常是 org.xml.sax.SAXParseException: cvc-elt.1: 不能 找到元素“bean”的声明。 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 来自类路径资源 [launch-context.xml] 的 XML 文档中的第 12 行 是无效的;嵌套异常是 org.xml.sax.SAXParseException: cvc-elt.1:找不到元素“beans”的声明。在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212) 在 org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126) 在 org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92) 在 org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) 在 org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397) 在 org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) 在 org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) 在 org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:282) 在 org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574) 在 com.my.path.invoker.JobTest.main(JobTest.java:25) 引起: org.xml.sax.SAXParseException: cvc-elt.1: 找不到声明 元素'豆'。在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131) 在 com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384) 在 com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318) 在 com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1916) 在 com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705) 在 com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400) 在 com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3103) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648) 在 com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140) 在 com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) 在 com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) 在 com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235) 在 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284) 在 org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) ... 16 更多

它基本上是说在第 11 行它找不到元素 bean 的声明。这是我的启动上下文。

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        
        xmlns:batch="http://www.springframework.org/schema/batch"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

        <bean id="placeholderConfig"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>file:batch.properties</value>
                    <value>file:application.properties</value>
                </list>
            </property>
        </bean>


        <context:component-scan base-package="com.my.path" />

        <import resource="classpath:/META-INF/spring/batch-context.xml" />
        <import resource="classpath:/META-INF/spring/module-context.xml" />


    </beans>

以及生成 jar 的 build.xml。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->
<project basedir="." default="jar" name="ERS2Utilities">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.6"/>
    <property name="source" value="1.6"/>
    <path id="CLASSPATH">
        <pathelement location="target/classes"/>
        <pathelement location="target/test-classes"/>
        <!-- 
        <pathelement location="src/main/resources/lib/antlr-2.7.7.jar"/>
        <pathelement location="src/main/resources/lib/antlr-runtime-3.2.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.instrument.tomcat-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.jms-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.js-2.1.1.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web.portlet-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web.servlet-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web.struts-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.webflow-2.1.1.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/slf4j-api-1.6.1.jar"/>
        <pathelement location="src/main/resources/lib/slf4j-api.jar"/>
        <pathelement location="src/main/resources/lib/slf4j-log4j12-1.6.1.jar"/>
        <pathelement location="src/main/resources/lib/commons-digester.jar"/>
         -->
        <pathelement location="src/main/resources/lib/com.springsource.org.aopalliance-1.0.0.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.aop-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.asm-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.aspects-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.beans-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.binding-2.1.1.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.context-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.context.support-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.core-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.expression-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.instrument-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.jdbc-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.orm-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.oxm-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.test-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.transaction-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/spring-batch-infrastructure-2.1.9.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/spring-batch-core-2.1.9.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/spring-batch-test-2.1.9.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/commons-beanutils.jar"/>
        <pathelement location="src/main/resources/lib/commons-collections-3.1.jar"/>
        <pathelement location="src/main/resources/lib/commons-dbcp-1.4.jar"/>
        <pathelement location="src/main/resources/lib/commons-lang-2.6.jar"/>
        <pathelement location="src/main/resources/lib/commons-logging-1.1.1.jar"/>
        <pathelement location="src/main/resources/lib/commons-pool-1.5.4.jar"/>
        <pathelement location="src/main/resources/lib/junit-4.7.jar"/>
        <pathelement location="src/main/resources/lib/ojdbc14.jar"/>
        <pathelement location="src/main/resources/lib/log4j-1.2.16.jar"/>
        <pathelement location="src/main/resources/lib/HashUtility.jar"/>
        <pathelement location="src/main/resources/lib/commons-io-2.4.jar"/>
        <pathelement location="src/main/resources/lib/mockito-all-1.9.5.jar"/>
    </path>
    <target name="init">
        <mkdir dir="target/classes"/>
        <mkdir dir="target/test-classes"/>
        <copy includeemptydirs="false" todir="target/classes">
            <fileset dir="src/main/resources">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
        <copy includeemptydirs="false" todir="target/classes">
            <fileset dir="src/main/java">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
        <copy includeemptydirs="false" todir="target/test-classes">
            <fileset dir="src/test/java">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
        <copy includeemptydirs="false" todir="target/classes">
            <fileset dir="src/test/resources">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="target/classes"/>
        <delete dir="target/test-classes"/>
    </target>



    <target depends="init" name="build-project">
        <echo message="$ant.project.name: $ant.file"/>
        <javac debug="true" debuglevel="$debuglevel" destdir="target/classes" source="$source" target="$target">
            <src path="src/main/resources"/>
            <classpath refid="CLASSPATH"/>
        </javac>
        <javac debug="true" debuglevel="$debuglevel" destdir="target/classes" source="$source" target="$target">
            <src path="src/main/java"/>
            <classpath refid="CLASSPATH"/>
        </javac>
        <javac debug="true" debuglevel="$debuglevel" destdir="target/test-classes" source="$source" target="$target">
            <src path="src/test/java"/>
            <classpath refid="CLASSPATH"/>
        </javac>
        <javac debug="true" debuglevel="$debuglevel" destdir="target/classes" source="$source" target="$target">
            <src path="src/test/resources"/>
            <classpath refid="CLASSPATH"/>
        </javac>
    </target>

    <!-- <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target> -->

    <target name="JobTest">
        <java classname="com.my.path.invoker.JobTest" failonerror="true" fork="yes">
            <arg line="launch-context.xml oiaExtractorJob"/>
            <classpath refid="CLASSPATH"/>
        </java>
    </target>

    <target name="jar" depends="build-project">
        <copy todir="build/main" file="src/main/resources/log4j.xml"/>
        <copy todir="build/main" file="application.properties"/>
        <copy todir="build/main" file="batch.properties"/>
        <copy todir="build/main" file="src/main/resources/ERSUtilities.sh"/>
        <copy todir="build/main" file="src/main/resources/ERSUtilities.bat"/>

        <jar destfile="build/main/ERS2SupportingUtilities.jar">
            <fileset dir="target/classes"/>
            <restrict>
             <name name="**/*.class"/>
             <archives>
               <zips>
                 <fileset dir="./src/main/resources/lib" includes="**/*.jar"/>
               </zips>
             </archives>
            </restrict>
            <manifest>
              <attribute name="Main-Class" value="com.my.path.invoker.JobTest"/>
                <attribute name="Class-Path" value="./lib/log4j-1.2.16.jar
                                                    ./lib/org.springframework.context-3.0.3RELEASE.jar
                                                    ./lib/org.springframework.asm-3.0.3.RELEASE.jar
                                                    ./lib/junit-4.7.jar
                                                    ./lib/org.springframework.orm-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.transaction-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.aspects-3.0.3.RELEASE.jar
                                                    ./lib/commons-pool-1.5.4.jar
                                                    ./lib/org.springframework.core-3.0.3.RELEASE.jar
                                                    ./lib/commons-logging-1.1.1.jar
                                                    ./lib/HashUtility.jar
                                                    ./lib/org.springframework.expression-3.0.3.RELEASE.jar
                                                    ./lib/commons-lang-2.6.jar
                                                    ./lib/org.springframework.instrument-3.0.3.RELEASE.jar
                                                    ./lib/mockito-all-1.9.5.jar
                                                    ./lib/com.springsource.org.aopalliance-1.0.0.jar
                                                    ./lib/ojdbc14.jar
                                                    ./lib/commons-io-2.4.jar
                                                    ./lib/commons-collections-3.1.jar
                                                    ./lib/org.springframework.jdbc-3.0.3.RELEASE.jar
                                                    ./lib/spring-batch-infrastructure-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.context.support-3.0.3.RELEASE.jar
                                                    ./lib/commons-dbcp-1.4.jar
                                                    ./lib/spring-batch-test-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.beans-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.oxm-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.aop-3.0.3.RELEASE.jar
                                                    ./lib/commons-beanutils.jar
                                                    ./lib/org.springframework.binding-2.1.1.RELEASE.jar
                                                    ./lib/spring-batch-core-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.test-3.0.3.RELEASE.jar
                                                    ./launch-context.xml
                                                    ./log4j.xml"
                />
            </manifest>
        </jar>
    </target>
</project>

我在这上面花了超过 3 天的时间,但无济于事。我已经确保我所做的 xsd 引用与我正在使用的 spring jar 的版本兼容。

任何帮助将不胜感激。

编辑:

同样的 launch-context.xml 在 eclipse 中工作,当它通过给定的 ant build.xml 构建时,它会抛出这个错误。

编辑 2:

我正在尝试将所有依赖的 jar 打包到一个胖 jar 中。我遇到过一些帖子表明我不能这样做。要引用打包的 jar,我需要 one-jar 或 eclipse 的 jarinjarloader。否则我将不得不将依赖的 jars 放在我的主 jar 之外。如果这是真正的问题,我将尝试这些选项并返回。同时,如果你们对此编辑有任何意见,请更新您的答案,我会将其标记为正确。

【问题讨论】:

【参考方案1】:

您选择的 XML 编辑器/验证器是否成功验证了您的应用程序上下文(您称为启动上下文的那个)?无效,因为http://www.springframework.org/schema/batch/spring-batch-2.1.xsd 无效。

但是,您发布的内容(只是摘录吗?)甚至不需要 batch 命名空间。因此,您可以将其删除,从而将其转换为有效的 XML。

【讨论】:

感谢您抽出宝贵时间回复。删除它仍然不能解决问题,我遇到了同样的错误。请原谅我缺乏经验,但我认为指向 spring-batch xsd 的链接是有效的,因为它指向一个架构位置。 还忘了提到通过 Eclipse 运行相同。当使用给定的build.xml 构建时,它会失败。我已经更新了问题。【参考方案2】:

在 ant 脚本中,您有 spring 3.0.3 的依赖项,而在应用程序上下文中,您包含 spring 3.0,这可能是问题吗?

【讨论】:

不,不是,我猜 xml 架构 3.0 对所有 spring 3.0.x 构建都有效。【参考方案3】:

如果您的执行服务器的防火墙阻止了对外部互联网的访问(这通常是一件好事),那么您需要使用 spring.handlers 和 spring.schemas 来帮助应用程序解析您的上下文文件。

这将引用您的框架库中存在的架构,而不是尝试从互联网上提取它们。

/META-INF/spring.handlers

http\://www.springframework.org/schema/batch=org.springframework.batch.core.configuration.xml.CoreNamespaceHandler
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jdbc=org.springframework.jdbc.config.JdbcNamespaceHandler

/META-INF/spring.schemas

http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd
http\://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd
http\://www.springframework.org/schema/batch/spring-batch-2.1.xsd=org/springframework/batch/core/configuration/xml/spring-batch-2.1.xsd

【讨论】:

以上是关于Spring批处理xmlns架构错误的主要内容,如果未能解决你的问题,请参考以下文章

SSH 架构

Spring boot 异常处理配置

大数据批处理框架 Spring Batch全面解析

如何根据特定的批处理作业定义 Spring 批处理云任务

批处理大数据框架Spring Batch全面解析

在 spring 配置文件错误中出现错误:与元素类型“beans”关联的属性“xmlns”的值不能包含“<”字符