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替換成自己的包)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM