【SPRINGMVC】Error creating bean with name 'projectController': Injection of autowired dependencies

古城微笑少年丶 2022-06-14 10:11 288阅读 0赞

错误代码:

  1. [2017-06-17 11:08:33] org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:351)
  2. <ERROR> Context initialization failed
  3. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hcepms.proj.project.service.ProjectServiceIntf com.hcepms.proj.project.controller.ProjectController.projectService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.hcepms.proj.project.service.ProjectServiceIntf] is defined: expected single matching bean but found 2: projectMemberDispatchServiceImpl,projectServiceImpl
  4. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
  5. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
  8. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
  9. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  10. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
  12. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:778)
  13. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:843)
  14. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
  15. at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
  16. at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
  17. at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
  18. at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4738)
  19. at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181)
  20. at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  21. at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
  22. at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
  23. at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
  24. at java.util.concurrent.FutureTask.run(Unknown Source)
  25. at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  26. at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  27. at java.lang.Thread.run(Unknown Source)
  28. Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hcepms.proj.project.service.ProjectServiceIntf com.hcepms.proj.project.controller.ProjectController.projectService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.hcepms.proj.project.service.ProjectServiceIntf] is defined: expected single matching bean but found 2: projectMemberDispatchServiceImpl,projectServiceImpl
  29. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
  30. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
  31. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
  32. ... 23 more
  33. Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.hcepms.proj.project.service.ProjectServiceIntf] is defined: expected single matching bean but found 2: projectMemberDispatchServiceImpl,projectServiceImpl
  34. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133)
  35. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1021)
  36. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
  37. ... 25 more

出现原因

代码本身没有做修改,我做了以下两个操作

  1. 将原来的包结构改变,去掉了一层中间包
  2. 将原有的controller、bean、service、impl复制了一套准备新模块的开发

于是出现了上述问题。

分析原因,我发现是由于ProjectService注入的时候发生的,于是全局搜索,我发现有两个地方均实现了ProjectService。

一个是我原有的实现类,一个是我复制过来的实现类。

解决办法

将复制过来的实现类,里面实现的service,改为新创建的service,clean工程,解决问题。

发表评论

表情:
评论列表 (有 0 条评论,288人围观)

还没有评论,来说两句吧...

相关阅读