天天看點

@org.springframework.beans.factory.annotation.Autowired(required=true)

錯誤: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘paymentOrderController’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.chaoge.service.paymentOrderService com.chaoge.controller.paymentOrderController.pos; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.chaoge.service.paymentOrderService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

由于自己平常做項目時喜歡喜歡各種嘗試,所有特别多奇葩錯誤!今天遇到了這個問題搞了半天,在網上也看了下别人的錯誤,和我的有點不一樣,突然想到配置springMvc隻配置了controller掃描注入,未配置service掃描注入,是以使用@Autowired無法注入報錯!分享一下,也給以後留個答案!

在springMvc檔案中配置以下代碼就可以了:

解決方法:

<!-- 掃描controller,service(controller,service層注入) -->
<context:component-scan base-package="com.chaoge.controller"/>
<context:component-scan base-package="com.chaoge.service"/>
           

繼續閱讀