java – 如何在BaseServiceImpl中自动装配@NoRepositoryBean BaseRepository

BaseRepository

@NoRepositoryBean
public interface BaseRepository<T extends BaseEntity, ID extends Serializable>
        extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

    T findByIdAndDeleteStatusFalse(ID id);
}

BaseServiceImpl

@Transactional(readOnly = true)
public abstract class BaseServiceImpl<T extends BaseEntity, ID extends Serializable> implements BaseService<T, ID> {

    @Autowired
    protected BaseRepository<T, ID> baseRepository;
}

applicationContext.xml中的jpa配置

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.coderbike.entity, com.coderbike.core.entity"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="showSql" value="false"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="database" value="MYSQL"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">false</prop>
        </props>
    </property>
</bean>

<!-- annotation transaction -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

<!-- scan repository package -->
<jpa:repositories base-package="com.coderbike.dao.jpa, com.coderbike.core.repository"
                  repository-impl-postfix="Impl"
                  transaction-manager-ref="transactionManager"
                  entity-manager-factory-ref="entityManagerFactory" />

启动tomcat时,发生错误

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.coderbike.core.repository.BaseRepository] found for dependency [com.coderbike.core.repository.BaseRepository<com.coderbike.entity.User, java.lang.Integer>]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
    ... 60 more

我在网上搜索了很长时间.但最流行的场景是创建BaseRepositoryImpl并自定义扩展JpaRepositoryFactory的BaseRepositoryFactory.那么我可以使用自动装配而不是上面的场景.

最佳答案 @NoRepositoryBean的概念不是为您创建特定存储库接口的代理实例.有很好的解释是
here.

这意味着您可以扩展此BaseRepository接口并提供和实现您自己的接口.完成后,在xml中声明该bean.

点赞