提问者:小点点

Spring JPA:XML配置-没有Repository的限定bean/找不到元素'jpa: repositories'的声明


我正在尝试将我的应用程序从JDBC移动到Spring JPA。

我扩展了CrudRepository,并创建了自己的DAO界面org.springframework.data

public interface IMyRepository extends CrudRepository<MyEntity, Long> {
// Queries
}

但是,IMyRepositotybean不会使用以下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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/data/repository
    http://www.springframework.org/schema/data/repository/spring-repository.xsd
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache.xsd">

<context:component-scan base-package="com.project.dao" />
<bean id="projectDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    autowire-candidate="true">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://ABC:3306/project" />
    <property name="username" value="userA" />
    <property name="password" value="1234" />
</bean>

<bean id="projectEntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="projectDataSource" />
    <property name="packagesToScan" value="com.project.dao" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
    <props>
        <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    </props>
    </property>   
</bean>

<bean id="projectJpaTransactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="projectEntityManagerFactory" />
</bean>

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

<bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    
<!-- <jpa:repositories base-package="com.project.dao"/> -->
</beans>

错误是:

No qualifying bean of type [com.project.dao.IMyRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

搜索了很多文章,发现需要添加

Caused by: org.xml.sax.SAXParseException; lineNumber: 68; columnNumber: 69; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jpa:repositories'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

我错过了什么吗?提前感谢!

更多信息,在模块的依赖项中,我有:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>
</dependencies>

并且父模块依赖于:spring-jdbc、spring-test、spring-core、spring-content-support、spring-orm版本4.1.6的所有内容。RELEASE


共2个答案

匿名用户

来自JPA存储库上的Spring Docs:

您需要添加此命名空间JPA存储库支持:

xmlns: jp a="http://www.springframework.org/schema/data/jpa"

以及JPA模式位置:

xsi:schemaLocation="http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"

这是您的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:jpa="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <jpa:repositories base-package="com.project.dao" />

</beans>

它应该工作。

匿名用户

在您的Application类上,您必须使用:

@EnableJpaRepositories(basePackages="您的存储库目录")

并将IMyRepositoty注释为

@存储库