spring service 注入問題

用spring、springmvc框架開發項目。junit測試的時候測試類報錯。java

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'aaa' must be of type [com.itech.ups.app.comManager.application.service.ComSysManagerService], but was actually of type [com.itech.ups.app.system.manager.application.service.ManagerServiceImp]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:374)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:443)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:417)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:542)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:304)
	... 25 more

整理以下圖 在service的impl上添加service(「xxx」) ![![![![![輸入圖片說明]spring

在controller類裏自動注入mvc

輸入圖片說明

當controller裏新定義的注入類別名和service註解裏的一致時,就會運行有service註解的service。app

而我這裏是兩個service,運行直接報錯,類型不匹配。框架

當名稱不一致時則會找到對應類型的service注入併成功運行。post

總結:注意多個service註解內的名稱與注入的service名稱一致可能會報錯。測試

相關文章
相關標籤/搜索