【问题标题】:Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]找不到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]
【发布时间】:2012-02-05 19:43:54
【问题描述】:

任何想法,可能导致此错误的原因是什么?

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 配置问题:找不到 Spring NamespaceHandler for XML 模式命名空间 [http://www.springframework.org/schema/data/jpa] 违规资源:ServletContext 资源 [/WEB-INF/spring/appServlet/servlet-context.xml]

这是我的“servle-context.xml”(缩进有些问题,但是文件太大了……):

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-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/tx 
                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    http://www.springframework.org/schema/data/jpa 
                    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.epam.mvc3.model" />
<context:component-scan base-package="com.epam.mvc3.controller" />

<!-- JPA -->
<beans:bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <beans:property name="location">
        <beans:value>resources/database.properties</beans:value>
    </beans:property>
</beans:bean>

<beans:bean id="myDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName">
        <beans:value>${jdbc.driverClassName}</beans:value>
    </beans:property>
    <beans:property name="url">
        <beans:value>${jdbc.url}</beans:value>
    </beans:property>
    <beans:property name="username">
        <beans:value>${jdbc.username}</beans:value>
    </beans:property>
    <beans:property name="password">
        <beans:value>${jdbc.password}</beans:value>
    </beans:property>
</beans:bean>

<beans:bean id="myEmf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <beans:property name="dataSource" ref="myDataSource" />

    <beans:property name="persistenceUnitName" value="application" />
    <beans:property name="persistenceXmlLocation"
        value="classpath*:META-INF/persistence.xml" />
    <beans:property name="jpaVendorAdapter" ref="hibernateVendor" />

    <beans:property name="loadTimeWeaver">
        <beans:bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"></beans:bean>
    </beans:property>
</beans:bean>

<beans:bean id="hibernateVendor"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</beans:bean>

<beans:bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <beans:property name="entityManagerFactory" ref="myEmf"></beans:property>
</beans:bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<!-- Spring Data configuration -->
<jpa:repositories base-package="com.epam.mvc3.repository"/>

</beans:beans>

如您所见,我指定了 jpa-schema 的路径。而且我不知道是什么问题

【问题讨论】:

  • 请发布您的 XML 的完整标题。
  • 确保类路径中有 Spring Data jars。

标签: java spring maven-3


【解决方案1】:

尝试替换

http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

通过

http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd

但如果您仍然使用 1.0-M1 版本,请先尝试更新。 DATAJPA-21

【讨论】:

  • 虽然这是可以接受的答案,但它并没有为我解决这个问题。我使用 spring-data-jpa 1.2.0.RELEASE,其余的 spring jar 是 3.1.3.RELEASE。关于 spring-data-commons-core - 我什至在我的 pom 中没有依赖项,但我可以在我的 m2 存储库中看到它以及 spring-data-commons-parent 和两个版本 1.4.0.RELEASE,我没有不知道为什么(也许这些是 spring-data-jpa 的一部分?)。有什么新见解吗?
  • @forhas:这似乎是一个不同的问题。因此,请创建一个新的 Stack Overflow 问题(描述您的问题、配置和异常,以及您目前尝试过的内容),并可能在此处留下您问题的链接作为评论。
  • 谢谢拉尔夫,我会这样做的。
【解决方案2】:

确保您的依赖项中有 spring-data-jpa

如果你使用 maven:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.7.0.RELEASE</version>
</dependency>

【讨论】:

    【解决方案3】:

    我通过解决我们项目中缺少的 spring-tx 依赖项来解决此问题。

    org.springframework:spring-tx

    【讨论】:

      【解决方案4】:

      问题是因为找不到 spring-jpa 模式。您可能有一些不包含架构信息的旧 jar。

      【讨论】:

      • 我正在使用“hibernate-core-3.6.3.Final.jar”、“hibernate-jpa-2.0-api-1.0.0.Final.jar”、“spring-data- commons-core-1.1.0.RELEASE.jar”,“spring-orm-3.0.5.RELEASE.jar”。你认为它们已经过时了吗?
      • @Eugene 你正在使用 spring-jpa 命名空间 - 你没有 spring-jpa jar 吗?你可以找到它here
      【解决方案5】:

      我知道这看起来很傻,但是如果您使用的是 IDEA 和 Maven,则应该检查以确保您已选中使用插件注册表,以便服务器也可以访问您的 jar 文件。

      我会发布一个屏幕截图,但我是新来的,没有足够的声望点。它确实有效,我已经与 IntelliJ 的支持团队进行了验证

      【讨论】:

        【解决方案6】:

        在遇到同样的问题一个多星期后,我找到了解决方案。如果您使用的是 Spring MVC + JPA + Maven,请使用这个 dispatcher-servlet.xml

        <?xml version="1.0" encoding="windows-1252"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:p="http://www.springframework.org/schema/p"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:mvc="http://www.springframework.org/schema/mvc"
               xmlns:aop="http://www.springframework.org/schema/aop"
               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/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc.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">
        
            <!-- Use @Component annotations for bean definitions -->
            <context:component-scan base-package="PACKAGE WHERE YOU CAN FIND YOUR SOURCE CODE"/>
        
            <!-- Use @Controller annotations for MVC controller definitions -->
            <mvc:annotation-driven />
        
            <!-- Add JPA support -->
            <bean id="emf" class=
                    "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="loadTimeWeaver">
                    <bean class=
                                  "org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
                </property>
            </bean>
        
            <!-- Add Transaction support -->
            <bean id="transactionManager"
                  class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory" ref="emf"/>
            </bean>
        
            <!-- Use @Transaction annotations for managing transactions -->
            <tx:annotation-driven transaction-manager="transactionManager" />
        
            <!-- View resolver -->
            <bean class=
        
        <!-- HERE YOU REPLACE PROPERTY VALUE TO MATCH YOUR VIEW FOLDER (JSP PAGES) -->
                          "org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/web/"/> 
                <property name="suffix" value=".jsp"/>
            </bean>
        
        </beans>
        

        【讨论】:

          【解决方案7】:

          为时已晚,但这解决了我的问题:

          <dependency>
              <groupId>org.springframework.data</groupId>
              <artifactId>spring-data-jpa</artifactId>
              <version>1.9.4.RELEASE</version>
          </dependency>
          

          【讨论】:

            猜你喜欢
            • 2013-11-06
            • 2017-02-18
            • 2015-10-29
            • 2013-04-16
            • 2019-08-08
            • 2012-01-21
            • 2013-11-01
            • 2011-11-03
            • 2014-04-19
            相关资源
            最近更新 更多