gpt4 book ai didi

Spring 框架: expected single matching bean but found 2

转载 作者:行者123 更新时间:2023-12-03 23:10:58 27 4
gpt4 key购买 nike

这个父类(super class) DAO:

public class CrudDAO{

}

这个子类:
@Repository
public class JnsTimeDao extends CrudDAO {

}

@Repository
public class BatchDAO extends CrudDAO {
}

这个 super 服务等级
@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDAO> {

@Autowired
protected D dao;
}

启动错误:

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

最佳答案

有 2 个 CrudDAO 类型的 bean .因此,Spring 将无法理解要注入(inject)哪个 bean。可以如下解决

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

}

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

注入(inject)时使用 @Qualifier
 @Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDAO> {

@Autowired
@Qualifier("batchDao")
protected D dao;
}

关于 Spring 框架: expected single matching bean but found 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47257480/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com