7

This super class DAO:

public class CrudDAO{

}

This child class:

@Repository
public class JnsTimeDao extends CrudDAO {

}

@Repository
public class BatchDAO extends CrudDAO {
}

this super service class

@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDAO> {

    @Autowired
    protected D dao;
}

startup error:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.gp.dao.CrudDAO] is defined: expected single matching bean but found 2: batchDAO,jnsTimeDao

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
HelloWorld
  • 147
  • 1
  • 1
  • 14
  • 1
    You have 2 beans of type `CrudDAO` one being `JnsTimeDao` and `BatchDao`. Hence Spring runtime is complaining of duplicate beans. You should ensure that Spring runtime will find either one of these as a qualified bean and wires it. – Shyam Baitmangalkar Nov 13 '17 at 05:30

1 Answers1

12

There are 2 beans of type CrudDAO. So, Spring won't be able to understand which bean to inject. Can be solved as follows

@Repository("jnsTimeDao")
public class JnsTimeDao extends CrudDAO {

}

@Repository("batchDao")
public class BatchDAO extends CrudDAO {
}

While injecting use @Qualifier

 @Transactional(readOnly = true)
    public abstract class CrudService<D extends CrudDAO> {

        @Autowired
        @Qualifier("batchDao")
        protected D dao;
    }
amdg
  • 2,211
  • 1
  • 14
  • 25