【问题标题】:spring-tx: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]spring-tx:无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/tx]
【发布时间】:2018-05-16 22:15:15
【问题描述】:

为什么我还是会收到这个?

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../beans/Character.xml]
Offending resource: class path resource [spring/config/beanLocations.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [spring/beans/Character.xml]

        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:72)

我已经阅读了很多关于这个问题的帖子,并根据他们配置了我的 xml-s。

我有问题的 xml 如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">

  <!-- Character Data Access Object -->
  <bean id="characterDao" class="com.got.common.dao.CharacterDao" >
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

  <tx:annotation-driven/>
  <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
</beans>

Spring-tx 包含在我的 pom.xml 中:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

beanLocations.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">


        <!-- Database Configuration -->
        <import resource="../database/DataSource.xml"/>
        <import resource="../database/hibernate.xml"/>

        <!-- Beans Declaration -->
        <import resource="../beans/Character.xml"/>

</beans>

程序集插件

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>com.got.common.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

我不确定是否也必须包含 aop,但它就在那里。 这个命名空间有什么问题?我想停止拔头发,所以请告诉我找不到这个命名空间的原因。

【问题讨论】:

  • 你确认 spring-tx 库确实放在你的war文件中了吗?
  • 另外,您的spring/config/beanLocations.xml 中有什么内容?某种嵌套定义?
  • 不是,但是 maven-assembly-plugin 应该把它放在那里。 (这是一个 jar 文件。)我在几秒钟内包含 beanLocations.xml
  • 好的。添加您的 maven-assembly-plugin 配置,它可能是相关的。到目前为止,您发布的 xml 配置没有任何问题,我怀疑您的构建过程存在问题,或者在 beanLocations.xml 中存在问题。
  • 好吧,再过一秒。

标签: java xml spring maven spring-mvc


【解决方案1】:

问题可能是你使用了assembly-plugin,它不理解spring handler机制,覆盖了spring.handlers文件中的handler定义。此线程中讨论了行为:Idea to avoid that spring.handlers/spring.schemas get overwritten when merging multiple spring dependencies in a single jar

为避免此问题,一种选择是使用 shade 插件来满足您的目的,如建议的 here

【讨论】:

  • 我希望这个答案能解决我的问题,所以我假设你的解决方案可以被接受,谢谢。
  • this 是关于程序集插件行为的错误条目:有趣的部分是它已在 2010 年声称已修复,并且还带有测试用例。这似乎不是因为人们似乎仍然遇到这个问题,所以证明问题并在那里发表评论的示例项目可以在程序集插件(修复所属的地方)中修复它。他们已经有一个过滤器来处理这个问题,但我想它不能正常工作。
  • 它有效,在经历了几个小时的痛苦之后,非常感谢您提供此链接。
猜你喜欢
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
  • 2019-01-27
  • 2013-10-04
  • 2011-01-10
相关资源
最近更新 更多