在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示。
错误提示如下所示:
控制台报错如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userServer'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.szh.easymvc.server.UserServer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
出现该报错问题,经过查找多个解决方法后,得到解决。如下:
1 原因分析:
bean不能创建不能自动装载一定出在配置问题上
2 解决步骤
a 检查 dao层(@param可不写) service层(@Server) controller层(@Controller)有没有加注解 如果没有加上一般就Ok了 如果加了 请看b步骤
b 检查 添加了注解dao层service层controller层对应的包有没有添加进针对这些注解的包扫描中 如果没有 加上就Ok(在spring的配置文件中检查)
(a)打开spring-context.xml配置文件
(b)找到class为org.mybatis.spring.mapper.MapperScannerConfigurer的bean
(c)在name为basePackage的property
(d)在对应的value下添加你的包就行了
如果上述这些步骤还没有解决的话,可以在spring-context.xml配置文件中加入如下语句:
让spring去扫描server包中的类。