spring-data-jpa 没有类型为“java.lang.Class”的限定Bean可用

ilmyapht  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(158)

我在我的项目中使用了spring data jpa(使用CrudRepository接口),并声明了我的存储库接口,如下所示:

/**
 * Repository implementation {@link CrudRepository}  for {@link Client} entity
 * @author rsone.
 */
public interface ClientRepository extends CrudRepository<Client, Integer> {
}

并有我的休眠配置如下:

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

<context:component-scan base-package="com.rsone.mga.*" />

<jpa:repositories base-package="com.rsone.mga.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="hibernateTransactionManager"/>

<bean id="datasource" parent="abstractDataSource">
    <property name="dataSourceName" value="mgaDS" />
    <property name="driverClass" value="${database.mga.jdbc.driver}" />
    <property name="jdbcUrl" value="${database.mga.jdbc.url}" />
    <property name="user" value="${database.mga.jdbc.user}" />
    <property name="password" value="${database.mga.jdbc.pass}" />
    <property name="preferredTestQuery" value="SELECT 1 FROM DUAL" />
</bean>    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="fetchSize" value="5000" />
    <property name="cacheQueries" value="true" />
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="datasource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        </props>
    </property>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="datasource"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <!-- spring based scanning for entity classes>-->
    <property name="packagesToScan" value="com.scor.mga.db"/>
</bean>

<!-- Enables the Hibernate @Transactional programming model -->
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

<bean id="hibernateTransactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <!--<property name="generateDdl" value="true"/>-->
    <property name="database" value="SQL_SERVER"/>
</bean>

但是当我尝试运行我的项目时,它给予我这个错误(知道我的仓库的基础包是com.rsone.mga.repository):

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.Class<org.springframework.data.repository.Repository<?, ?>>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1466)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)

编辑

我使用的是1.11.0版。spring-data-jpa的发布版本:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.11.0.RELEASE</version>
</dependency>
9q78igpj

9q78igpj1#

我在我的maven项目中也遇到了同样的问题。

所以下面的内容不起作用

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
</parent>

连同

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-cassandra</artifactId>
            <version>1.5.4.RELEASE</version>
</dependency>

解决方案

通过更新到最新的Sping Boot 版本解决了该问题:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>

这样做已经解决了问题。
看起来spring data标签与以前的版本不向后兼容(托管版本除外)。所以我也建议从spring-data-cassandra依赖项中删除version标签。显然Sping Boot 1.5.1.RELEASE管理spring-data-cassandra 1.5.0.RELEASE。

<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
</dependency>
cdmah0mi

cdmah0mi2#

这似乎不是您的配置文件中的错误。
看起来您在扫描的其中一个Bean中的java.lang.Class<org.springframework.data.repository.Repository<?, ?>>类型字段上有一个@Autowired。

相关问题