JPA - 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]

Posted

技术标签:

【中文标题】JPA - 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]【英文标题】:JPA - Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa] 【发布时间】:2017-02-18 01:05:45 【问题描述】:

Hibernate 和 Spring 的新手。所以我在网上复制粘贴了一些配置,剩下的自己做。

但是,当我尝试启动 Jetty 服务器时,我遇到了 Spring 错误。

找不到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]

这是什么意思?

    <?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:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:repository="http://www.springframework.org/schema/data/repository"
       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-4.0.xsd
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
           http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <jpa:repositories base-package="com.testproject.testpackage.example1.repository"
                      transaction-manager-ref="example1TransactionManager"
                      entity-manager-factory-ref="example1EntityManagerFactory">
        <repository:include-filter type="regex" expression=".*Repository"/>
    </jpa:repositories>

    <bean id="example1hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="poolName" value="example1Datasource"/>
        <property name="connectiontestprojectQuery" value="SELECT 1"/>
        <property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
        <property name="minimumIdle" value="5"/>
        <property name="maximumPoolSize" value="40"/>
        <property name="idleTimeout" value="2000"/>

        <property name="dataSourceProperties">
            <props>
                <prop key="url">$db.url</prop>
                <prop key="user">$db.username</prop>
                <prop key="password">$db.password</prop>
            </props>
        </property>
    </bean>
    <bean id="example1Datasource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <constructor-arg ref="example1hikariConfig"/>
    </bean>

    <bean id="hibernate.properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.show_sql">$hibernate.showSql</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
            </props>
        </property>
    </bean>

    <bean id="example1EntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="example1"/>
        <property name="dataSource" ref="example1Datasource"/>
        <property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.testproject.testpackage.example1.repository</value>
            </list>
        </property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
        <property name="jpaPropertyMap" ref="hibernate.properties"/>
    </bean>


    <bean id="example1TransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="example1EntityManagerFactory"/>
        <qualifier value="example1"/>
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven/>



</beans>

【问题讨论】:

【参考方案1】:

您的项目中似乎没有包含 spring-data-jpa-version.jar。

检查库或依赖项。

【讨论】:

【参考方案2】:

替换bean标签中的url

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

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

【讨论】:

以上是关于JPA - 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]的主要内容,如果未能解决你的问题,请参考以下文章

错误“无法找到 XML 模式命名空间的 Spring NamespaceHandler”

错误“无法找到 XML 模式命名空间的 Spring NamespaceHandler”

Spring 3.0 -- 无法为 XML 模式命名空间上下文找到 Spring NamespaceHandler

Spring,JMS - 无法找到 XML 模式命名空间的 Spring NamespaceHandler

无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/integration/twitter]

Spring 3.0 - 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/securit