5

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;
}

jpa configuration in applicationContext.xml

<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

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. So Can i use autowired instead of the scenario above.

Aaron189
  • 51
  • 1
  • 3

2 Answers2

1

Concept of @NoRepositoryBean is not to created proxy instance of you the particular repository interface. There is good explanation is here .

This means you are suppose to extend this BaseRepository interface and provide and implementation of your own. once done then declare that bean in your xml.

Community
  • 1
  • 1
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
0

This means that there's no BaseRepository bean available to fulfill that dependency.

Use annotation @Component or @Repository for BaseRepository and Spring will do work for you.

zexco2
  • 149
  • 2
  • 18