Spring JPA: XML Configuration – No qualifying bean of Repository / no declaration can be found for element 'jpa:repositories'

huangapple go评论100阅读模式
英文:

Spring JPA: XML Configuration - No qualifying bean of Repository / no declaration can be found for element 'jpa:repositories'

问题

I'm trying to move my application from JDBC to Spring JPA.

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

I have extended CrudRepository [of org.springframework.data.repository] and created my own DAO Interface

我扩展了CrudRepository [来自org.springframework.data.repository],并创建了自己的DAO接口。

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

But, IMyRepositoty bean is not getting created with below XML configuration.

但是,使用以下XML配置未创建IMyRepositoty bean。

<?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>

The error is:

错误是:

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)}

找不到类型为[com.project.dao.IMyRepository]的符合条件的bean,期望至少有1个bean符合此依赖项的自动装配候选项。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

After searching many articles, found that need to add <jpa:repositories base-package="com.project.dao"/> into XML. But that is resulting in another error as below

在查找了许多文章后,发现需要将<jpa:repositories base-package="com.project.dao"/>添加到XML中。但这导致了另一个错误,如下所示:

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'.

由于:org.xml.sax.SAXParseException; 行号:68; 列号:69; cvc-complex-type.2.4.c: 匹配通配符是严格的,但找不到元素 'jpa:repositories' 的声明。

Am I missing something? Thanks in advance!

我漏掉了什么吗?提前感谢!

英文:

I'm trying to move my application from JDBC to Spring JPA.

I have extended CrudRepository [of org.springframework.data.repository] and created my own DAO Interface

public interface IMyRepository extends CrudRepository&lt;MyEntity, Long&gt; {
// Queries
}

But, IMyRepositoty bean is not getting created with below XML configuration.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
xmlns:p=&quot;http://www.springframework.org/schema/p&quot; xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot; xmlns:cache=&quot;http://www.springframework.org/schema/cache&quot;
xmlns:jpa=&quot;http://www.springframework.org/schema/data/jpa&quot; xmlns:repository=&quot;http://www.springframework.org/schema/data/repository&quot;
xsi:schemaLocation=&quot;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">
&lt;context:component-scan base-package=&quot;com.project.dao&quot; /&gt;
&lt;bean id=&quot;projectDataSource&quot;
class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;
autowire-candidate=&quot;true&quot;&gt;
&lt;property name=&quot;driverClassName&quot; value=&quot;com.mysql.jdbc.Driver&quot; /&gt;
&lt;property name=&quot;url&quot; value=&quot;jdbc:mysql://ABC:3306/project&quot; /&gt;
&lt;property name=&quot;username&quot; value=&quot;userA&quot; /&gt;
&lt;property name=&quot;password&quot; value=&quot;1234&quot; /&gt;
&lt;/bean&gt;
&lt;bean id=&quot;projectEntityManagerFactory&quot;
class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;
&lt;property name=&quot;dataSource&quot; ref=&quot;projectDataSource&quot; /&gt;
&lt;property name=&quot;packagesToScan&quot; value=&quot;com.project.dao&quot; /&gt;
&lt;property name=&quot;jpaVendorAdapter&quot;&gt;
&lt;bean class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot; /&gt;
&lt;/property&gt;
&lt;property name=&quot;jpaProperties&quot;&gt;
&lt;props&gt;
&lt;prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;create-drop&lt;/prop&gt;
&lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
&lt;/props&gt;
&lt;/property&gt;   
&lt;/bean&gt;
&lt;bean id=&quot;projectJpaTransactionManager&quot;
class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;
&lt;property name=&quot;entityManagerFactory&quot; ref=&quot;projectEntityManagerFactory&quot; /&gt;
&lt;/bean&gt;
&lt;tx:annotation-driven transaction-manager=&quot;projectJpaTransactionManager&quot; /&gt;
&lt;bean id=&quot;persistenceExceptionTranslationPostProcessor&quot;
class=&quot;org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor&quot; /&gt;
&lt;!-- &lt;jpa:repositories base-package=&quot;com.project.dao&quot;/&gt; --&gt;
&lt;/beans&gt;

The error is:

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)}

After searching many articles, found that need to add &lt;jpa:repositories base-package=&quot;com.project.dao&quot;/&gt; into XML. But that is resulting in another error as below

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 &#39;jpa:repositories&#39;.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

Am I missing something? Thanks in advance!

More Info, In dependencies of module I have:

&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;version&gt;2.0.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;version&gt;5.1.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;version&gt;2.0.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;version&gt;2.0.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;

And parent module have dependency on : spring-jdbc, spring-test, spring-core, spring-context-support, spring-orm all of version 4.1.6.RELEASE

答案1

得分: 2

Spring文档关于JPA存储库

您需要添加此命名空间JPA Repository Support:

xmlns:jpa="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>

它应该可以工作。

英文:

From Spring Docs on JPA repository :

You need to add this namespace JPA Repository Support :

xmlns:jpa=&quot;http://www.springframework.org/schema/data/jpa&quot;

As well as JPA schema location :

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

This is missing from your xml file. That's why you are getting that schema error.


Try this :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:jpa=&quot;http://www.springframework.org/schema/data/jpa&quot;
xsi:schemaLocation=&quot;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">
&lt;jpa:repositories base-package=&quot;com.project.dao&quot; /&gt;
&lt;/beans&gt;

It should work.

答案2

得分: 0

在你的Application类中,你需要使用以下代码:

@EnableJpaRepositories(basePackages = "你的仓库目录")

并将IMyRepository标记为:

@Repository
英文:

On your Application class you have to use the:

> @EnableJpaRepositories(basePackages = "your repositories directory")

and annotate the IMyRepositoty as a

> @Repository

huangapple
  • 本文由 发表于 2020年7月29日 23:31:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63157145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定