Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.itcast.crm.service.BaseDictService cn.itcast.crm.controller.CustomerController.baseDictService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.itcast.crm.service.BaseDictService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
嚴重: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.itcast.crm.service.BaseDictService cn.itcast.crm.controller.CustomerController.baseDictService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.itcast.crm.service.BaseDictService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解決問題的思路:
1.如果您使用注釋進行配置钧忽,則可能會缺少cn.itcast.crm.service.BaseDictService實現(xiàn)的注釋(@Service或@Component等)逼肯。
2.如果您使用(僅)xml,您可能會錯過BaseDictService實現(xiàn)的<bean>定義篮幢。
3.如果您使用注釋并且實現(xiàn)注釋正確,請檢查掃描實現(xiàn)所在的包
(檢查各自組件的自動掃描組件<context:component-scan base-package = “XXXX”)
或者(Spring自動掃描<context:annotation-config/>)
4.代碼有無錯誤:例子如下,
BaseDictMapper在mapper文件下并在相關(guān)的mapper.xml進行相關(guān)配置缺菌,BaseDictService在service文件下并做了相關(guān)的spring配置文件做了對應(yīng)的配置。Service實現(xiàn)具有相同申明方法的mapper接口(而spring并不能掃描到mapper文件夾下的接口和類)搜锰,因此
BeanFactory在Spring Context中沒有找到bean的實例,最終導(dǎo)致Spring無法識別相應(yīng)的bean
public interface BaseDictMapper {
//根據(jù)類別代碼查詢數(shù)據(jù)
List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode);
}
public interface BaseDictService {
//根據(jù)類別代碼查詢
List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode);
}
public class BaseDictServiceImpl implements BaseDictMapper { //錯誤:應(yīng)該實現(xiàn)為BaseDictService
@Autowired
private BaseDictMapper baseDictMapper;
@Override
public List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode) {
List<BaseDict> list = this.baseDictMapper.queryBaseDictByDictTypeCode(dictTypeCode);
return list;
}
}