英文:
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<MyEntity, Long> {
// Queries
}
But, IMyRepositoty
bean is not getting created with below XML configuration.
<?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)}
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
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)
Am I missing something? Thanks in advance!
More Info, In dependencies of module I have:
<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>
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
您需要添加此命名空间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="http://www.springframework.org/schema/data/jpa"
As well as JPA schema location :
xsi:schemaLocation="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 :
<?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>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论