没有符合条件的bean类型&#39; java.lang.Class <org.springframework.data.repository.repository <?,?=“”>&gt;&#39;可得到

时间:2017-02-02 15:10:34

标签: spring spring-data-jpa

我在项目中使用spring data jpa(使用CrudRepository接口)并且已经声明了我的存储库接口:

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

让我的hibernate配置如下:

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

修改

我使用spring-data-jpa的版本1.11.0.RELEASE:

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

2 个答案:

答案 0 :(得分:2)

我在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>

<强>解决方案

通过更新到最新的Spring启动版本解决了这个问题:

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

这样做已经解决了问题。

看起来spring数据标签与以前的版本(托管版本除外)不向后兼容。 所以我还建议从spring-data-cassandra依赖项中删除version标签。显然Spring启动1.5.1.RELEASE管理spring-data-cassandra 1.5.0.RELEASE。

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

答案 1 :(得分:1)

配置文件中似乎没有错误。

看起来你在其中一个被扫描的bean中的java.lang.Class<org.springframework.data.repository.Repository<?, ?>>字段上有@Autowired。