【问题标题】:Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/batch]找不到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/batch]
【发布时间】:2025-11-25 19:10:01
【问题描述】:

情况

我正在使用 Spring Batch 为我们的数据仓库构建一个累积快照,但我遇到了一个我无法弄清楚的配置障碍。

我使用 Spring 模板项目 创建了一个带有 STS(SpringSource Tool Suite 2.8.1)的简单 Spring Batch 项目。这是我创建的两个 xml 配置文件:

launch-context.xml

<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:property-placeholder location="classpath:batch.properties" />

<context:component-scan base-package="edu.kdc.visioncards" />

<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="${batch.schema.script}" />
</jdbc:initialize-database>

<batch:job-repository id="jobRepository" />

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

module-context.xml

 <?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"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<description>Example job to get you started. It provides a skeleton for a typical batch application.</description>

<batch:job id="job1">
    <batch:step id="step1"  >           
        <batch:tasklet transaction-manager="transactionManager" start-limit="100" >
            <batch:chunk reader="reader" writer="writer" commit-interval="1" />
        </batch:tasklet>
    </batch:step>
</batch:job> 

</beans>

问题

我运行以下命令来编译和打包项目:mvn clean compile install

然后我这样做:

  1. cdtarget 文件夹

  2. 通过 CommandLineJobRunner 运行作业:java -jar batchprimer-1.0.jar META-INF/spring/module-context.xml job1

我收到以下错误:

C:\stsworkspace\BatchPrimer\target>java -jar batchprimer-1.0.jar META-INF/spring
/module-context.xml job1
2011-12-15 12:03:53,421 INFO [org.springframework.context.support.ClassPathXmlAp
plicationContext] - <Refreshing org.springframework.context.support.ClassPathXml
ApplicationContext@84abc9: startup date [Thu Dec 15 12:03:53 EST 2011]; root of
context hierarchy>
2011-12-15 12:03:53,468 INFO [org.springframework.beans.factory.xml.XmlBeanDefin
itionReader] - <Loading XML bean definitions from class path resource [META-INF/
spring/module-context.xml]>
2011-12-15 12:03:53,796 ERROR [org.springframework.batch.core.launch.support.Com
mandLineJobRunner] - <Job Terminated in error: Configuration problem: Unable to
locate Spring NamespaceHandler for XML schema namespace [http://www.springframew
ork.org/schema/batch]
Offending resource: class path resource [META-INF/spring/module-context.xml]
>
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Config
uration problem: Unable to locate Spring NamespaceHandler for XML schema namespa
ce [http://www.springframework.org/schema/batch]
Offending resource: class path resource [META-INF/spring/module-context.xml]

        at org.springframework.beans.factory.parsing.FailFastProblemReporter.err
or(FailFastProblemReporter.java:68)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderC
ontext.java:85)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderC
ontext.java:80)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.er
ror(BeanDefinitionParserDelegate.java:284)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.pa
rseCustomElement(BeanDefinitionParserDelegate.java:1335)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.pa
rseCustomElement(BeanDefinitionParserDelegate.java:1328)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentRe
ader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentRe
ader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registe
rBeanDefinitions(XmlBeanDefinitionReader.java:493)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadB
eanDefinitions(XmlBeanDefinitionReader.java:390)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:334)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:302)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:126)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:92)
        at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:467)
        at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:397)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:83)
        at org.springframework.batch.core.launch.support.CommandLineJobRunner.st
art(CommandLineJobRunner.java:282)
        at org.springframework.batch.core.launch.support.CommandLineJobRunner.ma
in(CommandLineJobRunner.java:574)

其他东西

在使用 mvn install 命令填充的目标文件夹中,我有如下内容:

  1. 一个classes文件夹
  2. 一个包含我所有 maven 依赖项的 lib 文件夹
  3. 一个ma​​ven-archiver文件夹
  4. 测试类文件夹
  5. 一个batchprimer-1.0.jar

在罐子内,在 META-INF/MANIFEST.MF 中。我有如下:

Manifest-Version: 1.0

Archiver-Version: Plexus Archiver

Created-By: Apache Maven

Built-By: dpardo

Build-Jdk: 1.6.0_20

Main-Class: org.springframework.batch.core.launch.support.CommandLineJ
 obRunner

Class-Path: lib/spring-jdbc-3.0.6.RELEASE.jar lib/spring-beans-3.0.6.R
 ELEASE.jar lib/spring-core-3.0.6.RELEASE.jar lib/spring-asm-3.0.6.REL
 EASE.jar lib/commons-logging-1.1.1.jar lib/spring-tx-3.0.6.RELEASE.ja
 r lib/aopalliance-1.0.jar lib/spring-aop-3.0.6.RELEASE.jar lib/spring
 -context-3.0.6.RELEASE.jar lib/spring-expression-3.0.6.RELEASE.jar li
 b/cglib-nodep-2.2.jar lib/spring-batch-core-2.1.7.RELEASE.jar lib/spr
 ing-batch-infrastructure-2.1.7.RELEASE.jar lib/xstream-1.3.jar lib/xp
 p3_min-1.1.4c.jar lib/jettison-1.1.jar lib/commons-io-1.4.jar lib/com
 mons-dbcp-1.2.2.jar lib/commons-pool-1.3.jar lib/hsqldb-1.8.0.7.jar l
 ib/aspectjrt-1.6.8.jar lib/aspectjweaver-1.6.8.jar lib/log4j-1.2.14.j
 ar lib/slf4j-log4j12-1.5.8.jar lib/slf4j-api-1.5.8.jar lib/mysql-conn
 ector-java-5.1.3.jar

所以我不认为这是一个类路径问题,因为在控制台中它似乎正在寻找罐子。

我尝试过的

根据错误,我粘贴了以下网址

http://www.springframework.org/schema/batch/

Spring Batch 的命名空间地址似乎是正确的。我还没有发现任何有用的东西。我在某处读到“某事”可能会覆盖文件的命名空间,但并不真正理解为什么、什么和/或如何解决它。

这是开箱即用的,如果没有进行任何修改、附加组件或任何东西,它就已经无法工作。有什么想法吗?

谢谢

解决方案

除了下面标记为正确的解决方案。我还必须将 launch-context.xml 从组件扫描更改为常规弹簧接线,如下所示:

<!-- Commented this 
<context:property-placeholder location="classpath:batch.properties" />  


<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="${batch.schema.script}" />
</jdbc:initialize-database>
-->
<!-- Replace it with this below -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${batch.jdbc.driver}" />
    <property name="url" value="${batch.jdbc.url}" />
    <property name="username" value="${batch.jdbc.user}" />
    <property name="password" value="${batch.jdbc.password}" />
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    lazy-init="true">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="placeholderProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:batch.properties" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="1" />
</bean>

【问题讨论】:

  • 我认为 Java 只定位 META-INF/MANIFEST.MF(而不是 META-INF/spring/MANIFEST.MF)——也许 Java 正在使用另一个清单文件。检查here 如何自定义您的清单文件。我还找到了this post,这在某种程度上是相关的,但你使用的是最新的 Spring 版本,这种组合对我有用。
  • MANIFEST.MF 一直在 META-INF 下,抱歉我误导了。我会快速更新帖子
  • 一般我放弃。如果您没有什么特别的,请将您的 jar+lib 上传到文件共享。我将尝试在带有断点的调试器下运行它 DefaultNamespaceHandlerResolver#getHandlerMappings() 方法(顺便说一句,尝试将日志级别提高到 DEBUG 并查看此类报告的内容;应该看起来像 this)。

标签: spring spring-batch


【解决方案1】:

通过 CommandLineJobRunner 运行作业:java -jar batchprimer-1.0.jar META-INF/spring/module-context.xml job1

即使有一个完整的目标文件夹,您也必须为 java 命令提供类路径信息,为了简化配置,您可以尝试使用多合一的可执行 jar,例如使用 maven-shade-plugin 或带有所有需要的库的可执行 shell 脚本 (.bat/.sh),例如appassembler-maven-plugin

maven-shade-plugin 示例配置(创建额外的 jar):

<plugin>
    <!-- create an all-in-one executable jar with maven-shade-plugin
         bound to phase:package 
         special handling for spring.handlers/spring.schemas files
         to prevent overwriting (maven-shade-plugin joins them to
         one file) 

         usage:
         cd to <project>/target
         java -jar hello-world-java-1.0-SNAPSHOT-executable.jar spring/batch/job/hello-world-job.xml helloWorldJob
         -->                     
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                </transformers>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <!-- configures the suffix name for the executable jar
                     here it will be '<project.artifact>-<project.version>-executable.jar'-->
                <shadedClassifierName>executable</shadedClassifierName>
            </configuration>
        </execution>
    </executions>
</plugin>

appassembler 配置示例(创建子文件夹结构和 .bat/.sh):

<plugin>
    <artifactId>appassembler-maven-plugin</artifactId>
    <groupId>org.codehaus.mojo</groupId>
    <version>1.1.1</version>
    <configuration>
        <repositoryLayout>flat</repositoryLayout>
        <installArtifacts>false</installArtifacts>
        <target>${project.build.directory}/appassembler</target>
        <defaultJvmSettings>
            <initialMemorySize>512M</initialMemorySize>
            <maxMemorySize>1024M</maxMemorySize>
            <extraArguments>
                <extraArgument>-Dlog4j.configuration=../etc/log4j/log4j.properties</extraArgument>
            </extraArguments>
        </defaultJvmSettings>
        <configurationDirectory>etc</configurationDirectory>
        <daemons>
            <daemon>
                <id>applicationName</id>
                <mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
                <commandLineArguments>
                    <commandLineArgument>spring/job-runner.xml</commandLineArgument>
                    <commandLineArgument>helloWorldJob</commandLineArgument>
                    <commandLineArgument>input.file.pattern=file:.../**/*.txt</commandLineArgument>
                </commandLineArguments>
                <platforms>
                    <platform>booter-unix</platform>
                    <platform>booter-windows</platform>
                </platforms>
            </daemon>
        </daemons>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>generate-daemons</goal>
                <goal>create-repository</goal>
            </goals>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 我在我的 pom 中添加了上面的两个 xml,并运行以下命令:java -jar batchprimer-1.0.jar META-INF/spring/module-context.xml job1,我得到 Failed to load Main-Class manifest attribute from batchprimer-1.0.jar 有什么想法吗?
  • 哦,好的,batchprimer-1.0.jar 中没有 Manifest。我不知道为什么这个阴影插件会生成两个罐子。我在它生成java -jar batchprimer-1.0-executable.jar META-INF/spring/module-context.xml job1 的另一个 jar 上运行相同的命令,它引发了 Spring 错误。我会解决这个问题,虽然很奇怪,因为它是一个简单的项目,我什么都没碰
  • shade 插件创建了一个额外的 jar,可执行文件,稍微改变了描述,但这也在插件页面上说明
  • +1 用于解决方法,但我不会为我的 Web 应用程序将所有 jar 合并为一个,Spring 仍然可以解析批处理命名空间。
  • 感谢所有为解决我的问题做出贡献的人。我通过使用第一个选项maven-shade-plugin解决了它,我的最终命令是java-jar batchprimer-1.0-executable.jar launch-context.xml job1。也许我什至一开始就把命令行中的 xml 弄错了。
【解决方案2】:

我正在使用 spring batch 版本 3.X.X 和其他 spring 依赖版本 4.X.X ,并收到上述错误。经过一些试验和错误后,我发现维护所有类似的弹簧版本解决了我的问题。因此,建议您使用 spring 批处理版本 3.X.X 或将 spring-jdbc 和 spring-tx 的 spring 依赖版本设置为 2.X.X

问候,狂欢

【讨论】:

    【解决方案3】:

    您需要运行时类路径中的 spring-batch.jar。它提供了一个能够处理特定命名空间的命名空间处理程序。

    【讨论】:

    • 我有 spring-batch-core-2.1.7.RELEASE.jarspring-batch-infrastructure-2.1.7.RELEASE.jar 在其他 spring-XXX-3.0.6.RELEASE.jar(s) 和其他依赖项中。我仍然有这个错误。
    • 我在目标文件夹中运行 java -jar 命令,该文件夹中充满了类和一个包含所有 jar 的 lib 文件夹。清单文件有一个带有值的类路径条目:lib/spring-batch-core-2.1.7 等。我将把它添加到我的帖子中
    【解决方案4】:

    这个错误是因为没有看到 spring-batch-core-2.1.7.RELEASE.jar 中的 META-INF/spring.handlers 文件引起的。该文件包含

    http\://www.springframework.org/schema/batch=org.springframework.batch.core.configuration.xml.CoreNamespaceHandler
    

    看起来像是类路径问题或其他 JAR(例如 spring-context)中的 spring.handlers 文件以某种方式优先。

    【讨论】:

    • 谢谢,如何控制订单?
    • @EmersonFarrugia:优先级不应起任何作用:扫描所有资源并注册命名空间处理程序。
    • @dma_k 所以你知道我能做些什么吗?
    • @Viriato:检查我在标题中的回复:首先确保您的根 MANIFEST.MF 是正确的。
    【解决方案5】:

    在我的情况下,它只是缺少 spring 事务依赖——或者可能指向一个旧的。用 3.1 替换它可以消除错误。

    【讨论】:

    • 感谢您的建议 - 对我来说,这是一个缺少的依赖项,而不是一个奇怪的设置问题。我忘记了 spring-security-config。