使用SpringMVC框架,这条完整的error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountCtrl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bing.mapper.wxcms.service.AccountService com.bing.mapper.wxcms.ctrl.AccountCtrl.accountService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bing.mapper.wxcms.service.AccountService] 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)}
报错中异常为NoSuchBeanDefinitionException,指出没有找到要注入的内容(service或dao),从两方面着手:
1、检查下在spring配置中,是否将对应的包加入扫描。
dao配置:
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.bing.mapper.**.dao"/>
</bean>
扫描service包下所有使用注解的类型
<context:component-scan base-package="com.bing.mapper.**.service"/>
2、检查对应的注解有没有加上,可能没加@service
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public List<Account> listForPage(Account searchEntity) {
return accountDao.listForPage(searchEntity);
}
}
---------------------
作者:夏日踩冰
来源:CSDN
原文:https://blog.csdn.net/a411358606/article/details/54426424