專案中spring容器載入的問題
阿新 • • 發佈:2018-11-21
今天做一個專案採用的是傳統架構,沒有采用分散式,部署時出現了異常,資訊是:
org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type found for dependency
error creating bean with name 'xxx': cannot resolve refere 'xxx'
expected at least 1 bean which qualifies as autowire candidate for this dependency
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
無法建立ItemService,因為找不到
首先檢查ItemService的實現類上是否加了@Service註解,發現加了
@Service public class ItemServiceImpl implements ItemService {
然後檢查spring的配置檔案是否有註解掃描器,發現有
<!--註解掃描器--> <context:component-scan base-package="com.rui.service"/>
最後檢查web.xml是否載入了spring容器,發現沒有,於是新增程式碼
<!-- 載入spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:com.rui/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
錯誤原因找到了,在web.xml中僅配置了springmvc的前端控制器,沒有載入spring容器。