简体   繁体   English

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

[英]how to autowire @NoRepositoryBean BaseRepository in BaseServiceImpl

BaseRepository BaseRepository

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

    T findByIdAndDeleteStatusFalse(ID id);
}

BaseServiceImpl BaseServiceImpl

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

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

jpa configuration in applicationContext.xml 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" />

when starting tomcat, error occurr 启动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

I am searching for a long time on net. 我在网上搜索了很长时间。 but the most popular scenario is that create a BaseRepositoryImpl and customize BaseRepositoryFactory which extends JpaRepositoryFactory. 但最流行的场景是创建BaseRepositoryImpl并自定义扩展JpaRepositoryFactory的BaseRepositoryFactory。 So Can i use autowired instead of the scenario above. 那么我可以使用自动装配而不是上面的场景。

Concept of @NoRepositoryBean is not to created proxy instance of you the particular repository interface. @NoRepositoryBean的概念不是为您创建特定存储库接口的代理实例。 There is good explanation is here . 有很好的解释是在这里

This means you are suppose to extend this BaseRepository interface and provide and implementation of your own. 这意味着您可以扩展此BaseRepository接口并提供和实现您自己的接口。 once done then declare that bean in your xml. 完成后,在xml中声明该bean。

This means that there's no BaseRepository bean available to fulfill that dependency. 这意味着没有可用于实现该依赖关系的BaseRepository bean。

Use annotation @Component or @Repository for BaseRepository and Spring will do work for you. 为BaseRepository使用注释@Component或@Repository,Spring将为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM