報錯先貼出來:
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替換成自己的包)