用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名称一致可能会报错。测试