SSM Service自动注入失败
报错先贴出来:
Error creating bean with name 'testController':
Unsatisfied dependency expressed through field 'userInfoService': No qualifying bean of type [bcs.wms.service.UserInfoService] found for dependency [bcs.wms.service.UserInfoService]:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)};
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [bcs.wms.service.UserInfoService] found for dependency [bcs.wms.service.UserInfoService]:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
在控制器自动注入Service时报以上错误。
1、首先要确保语法正确(包括已添加@Service注解)
2、其次要看配置文件有没有配置相关的自动扫描,并且很可能不止要在一个文件里要配置。
spring的配置文件要配置,spring-mvc的配置文件也要配置。本次出错正是因为没有在spring-mvc的配置文件里配置,才导致controller层自动注入Service时出错。
解决方案:
在spring、springmvc的配置文件中,都要加上:
<context:component-scan base-package="bcs.wms.service"/> (bcs.wms.service替换成自己的包)