@Autowired 时的异常

本文记录了一次Spring框架中依赖注入失败的问题排查过程,详细分析了由于未正确配置@Service或@Component注解导致的依赖注入异常,并给出了正确的字段类型声明方式。

14:53:41,390 ERROR TestContextManager:258 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1465ca2] to prepare test instance [com.wangyu.manager.impl.OrgnizationManagerImplTest@6bba64]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wangyu.manager.impl.OrgnizationManagerImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:52)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:50)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 19 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 21 more



你的OrgnizationManagerImpl必须以@Service或@Component注解才行。


15:51:13,265 ERROR TestContextManager:258 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@17e60a1] to prepare test instance [com.wangyu.manager.impl.OrgnizationManagerImplTest@1f9f0f2]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wangyu.manager.impl.OrgnizationManagerImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 19 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 21 more
15:51:13,359 ERROR TestContextManager:258 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@17e60a1] to prepare test instance [com.wangyu.manager.impl.OrgnizationManagerImplTest@1d4eeb5]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wangyu.manager.impl.OrgnizationManagerImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 19 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 21 more
15:51:13,375 ERROR TestContextManager:258 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@17e60a1] to prepare test instance [com.wangyu.manager.impl.OrgnizationManagerImplTest@83e35b]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wangyu.manager.impl.OrgnizationManagerImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 19 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 21 more
15:51:13,437 ERROR TestContextManager:258 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@17e60a1] to prepare test instance [com.wangyu.manager.impl.OrgnizationManagerImplTest@c378f6]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wangyu.manager.impl.OrgnizationManagerImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 19 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 21 more
15:51:13,453 ERROR TestContextManager:258 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@17e60a1] to prepare test instance [com.wangyu.manager.impl.OrgnizationManagerImplTest@13a83cc]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wangyu.manager.impl.OrgnizationManagerImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 19 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 21 more
15:51:13,453 ERROR TestContextManager:258 - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@17e60a1] to prepare test instance [com.wangyu.manager.impl.OrgnizationManagerImplTest@a2d304]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wangyu.manager.impl.OrgnizationManagerImplTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wangyu.manager.impl.OrgnizationManagerImpl com.wangyu.manager.impl.OrgnizationManagerImplTest.orgnizationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 19 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.wangyu.manager.impl.OrgnizationManagerImpl] is defined: Unsatisfied dependency of type [class com.wangyu.manager.impl.OrgnizationManagerImpl]: expected at least 1 matching bean
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 21 more
15:51:13,468 INFO GenericApplicationContext:815 - Closing org.springframework.context.support.GenericApplicationContext@1c86be5: display name [org.springframework.context.support.GenericApplicationContext@1c86be5]; startup date [Fri Feb 20 15:51:05 CST 2009]; root of context hierarchy
15:51:13,468 INFO DefaultListableBeanFactory:421 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@165c7f6: defining beans [userBean,annotationPropertyConfigurer,dataSource,sessionFactory,transactionManager,txAdvice,org.springframework.aop.config.internalAutoProxyCreator,allManagerMethod,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,orgnizationManagerImpl,securityManager,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,userDAO,orgnizationDAO,userManager,loggerListener,_authenticationManager,_filterChainProxy,_httpSessionContextIntegrationFilter,_filterChainProxyPostProcessor,_filterChainList,_securityContextHolderAwareRequestFilter,_accessManager,_portMapper,_exceptionTranslationFilter,_filterSecurityInterceptor,_sessionFixationProtectionFilter,_logoutFilter,_basicAuthenticationEntryPoint,_basicAuthenticationFilter,_formLoginFilter,_formLoginEntryPoint,_entryPointInjectionBeanPostProcessor,_userServiceInjectionPostProcessor,org.springframework.security.providers.dao.DaoAuthenticationProvider#0,org.springframework.security.config.AuthenticationProviderBeanDefinitionParser$AuthenticationProviderCacheResolver#0,accessDecisionManager,resourceSecurityInterceptor,secureResourceFilterInvocationDefinitionSource]; root of factory hierarchy
5328 [Thread-5] INFO org.hibernate.impl.SessionFactoryImpl - closing
15:51:13,531 INFO AnnotationSessionFactoryBean:246 - Closing Hibernate SessionFactory


自动写入的时候把接口写成实现类了
@Autowired
private OrgnizationManagerImpl orgnizationManager;
应该是
@Autowired
private IOrgnizationManager orgnizationManager;
<think>好的,用户现在需要对多个通过@Autowired注入的Bean进行存在性的单元测试。首先,我需要考虑Spring中如何验证Bean是否成功注入。通常,单元测试可以使用Spring Boot的测试支持,比如@SpringBootTest,这样会加载应用上下文,自动注入Bean。如果Bean不存在,注入会抛出异常,测试失败。 用户的问题是关于这些EnhanceTaskExecutor类型的Bean是否存在,所以测试的核心是确保在应用上下文中这些Bean被正确注册和注入。需要注意的是,用户可能使用了自定义的TaskExecutor,可能是通过@Bean或其他方式配置的,测试需要确保这些配置被正确加载。 首先,测试类需要使用@SpringBootTest注解,这样会启动Spring上下文。然后,对于每个@Autowired的字段,只要上下文启动成功且没有注入异常,说明这些Bean存在。但为了更明确,可以在测试方法中检查这些字段是否为null,如果为null则测试失败。 另外,可能需要考虑测试的范围,比如是否需要加载所有配置,或者是否有条件配置的情况。如果某些Bean是在特定条件下才会创建(比如@ConditionalOnProperty),测试需要确保这些条件满足。 还需要注意,如果使用JUnit 5,测试类需要用@Test注解方法,并且使用@SpringBootTest来启动上下文。此外,可能需要处理可能的异常,比如NoSuchBeanDefinitionException,但在自动注入,如果Bean不存在,Spring会在上下文加载抛出异常,导致测试失败,所以通常不需要显式捕获。 总结步骤应该是:创建测试类,使用@SpringBootTest,自动注入这些Bean,然后在测试方法中断言这些Bean不为null。这样就能验证它们的存在性。 </think>在 Spring 中验证多个 `@Autowired` 注入的 Bean 是否存在,核心思路是通过 **Spring Boot 测试框架** 加载应用上下文,并检查注入的 Bean 是否为 `null`。以下是具体实现步骤、示例代码及注意事项: ### 实现步骤 1. **引入测试依赖**:确保项目中包含 `spring-boot-starter-test` 依赖(默认已包含在 Spring Boot 项目中),提供 `@SpringBootTest`、`JUnit` 等测试支持。 2. **编写测试类**:使用 `@SpringBootTest` 注解标记测试类,Spring Boot 会自动加载应用上下文(包含所有已配置的 Bean)。 3. **自动注入目标 Bean**:在测试类中通过 `@Autowired` 注入需要验证的所有 `EnhanceTaskExecutor` 类型的 Bean。 4. **断言 Bean 非空**:在测试方法中,使用 JUnit 的 `assertNotNull` 断言每个注入的 Bean 不为 `null`。若某个 Bean 不存在,Spring 会在上下文加载抛出 `NoSuchBeanDefinitionException`,导致测试失败;若注入后为 `null`(罕见),断言会显式失败。 ### 示例代码 假设项目使用 JUnit 5(Spring Boot 2.4+ 默认),测试类如下: ```java import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertNotNull; @SpringBootTest // 加载完整应用上下文(默认加载主配置类) public class TaskExecutorBeanExistenceTest { // 需要验证的 Bean(与用户代码中的 @Autowired 字段一致) @Autowired private EnhanceTaskExecutor grpcServerExecutor; @Autowired private EnhanceJUCScheduledThreadPoolExecutor executorService; @Autowired private EnhanceTaskExecutor signalingAsyncTaskExecutor; @Autowired private EnhanceTaskExecutor passthroughAsyncTaskExecutor; @Autowired private EnhanceTaskExecutor preConnectionPassthroughExecutor; @Autowired private EnhanceTaskExecutor preConnectionRequestRelayExecutor; @Autowired private EnhanceTaskExecutor streamManageExecutor; @Autowired private EnhanceTaskExecutor userAndDeviceInfoRequestExecutor; @Autowired private EnhanceTaskExecutor prolongExecutor; @Autowired private EnhanceTaskExecutor playerNumExecutor; @Autowired private EnhanceTaskExecutor trackSessionExecutor; @Autowired private EnhanceTaskExecutor relayClientExecutor; @Test void testAllTaskExecutorsExist() { // 断言每个 Bean 非空 assertNotNull(grpcServerExecutor, "grpcServerExecutor Bean 未找到"); assertNotNull(executorService, "executorService Bean 未找到"); assertNotNull(signalingAsyncTaskExecutor, "signalingAsyncTaskExecutor Bean 未找到"); assertNotNull(passthroughAsyncTaskExecutor, "passthroughAsyncTaskExecutor Bean 未找到"); assertNotNull(preConnectionPassthroughExecutor, "preConnectionPassthroughExecutor Bean 未找到"); assertNotNull(preConnectionRequestRelayExecutor, "preConnectionRequestRelayExecutor Bean 未找到"); assertNotNull(streamManageExecutor, "streamManageExecutor Bean 未找到"); assertNotNull(userAndDeviceInfoRequestExecutor, "userAndDeviceInfoRequestExecutor Bean 未找到"); assertNotNull(prolongExecutor, "prolongExecutor Bean 未找到"); assertNotNull(playerNumExecutor, "playerNumExecutor Bean 未找到"); assertNotNull(trackSessionExecutor, "trackSessionExecutor Bean 未找到"); assertNotNull(relayClientExecutor, "relayClientExecutor Bean 未找到"); } } ``` ### 注意事项 1. **上下文加载范围**:`@SpringBootTest` 默认会加载主应用类(如 `@SpringBootApplication` 标记的类)的所有配置。若 Bean 是在特定配置类(如 `@Configuration`)中定义的,需确保该配置被主应用类扫描到(或通过 `classes` 属性指定)。 2. **条件 Bean 的处理**:若部分 Bean 是通过 `@ConditionalOnProperty`、`@ConditionalOnBean` 等条件注解注册的,需在测试环境中满足这些条件(例如通过 `@TestPropertySource` 配置属性)。 3. **测试性能优化**:若加载完整上下文较慢,可通过 `@SpringBootTest(classes = {SpecificConfig.class})` 仅加载必要的配置类,缩小上下文范围。 4. **异常处理**:若某个 Bean 不存在,Spring 会在上下文启动抛出 `NoSuchBeanDefinitionException`,测试直接失败(无需等待断言)。断言 `assertNotNull` 主要用于防御性检查(如注入错误导致的 `null`)。 5. **依赖验证**:若 Bean 本身依赖其他未正确配置的 Bean(如 `@Autowired` 的其他组件不存在),上下文加载也会失败,间接验证了依赖链的完整性。 ### 扩展:验证 Bean 类型 若需进一步验证 Bean 的实际类型(例如是否为 `EnhanceTaskExecutor` 的正确子类),可在断言中添加类型检查: ```java @Test void testBeanTypes() { assertTrue(grpcServerExecutor instanceof EnhanceTaskExecutor, "grpcServerExecutor 类型错误"); assertTrue(executorService instanceof EnhanceJUCScheduledThreadPoolExecutor, "executorService 类型错误"); // 其他 Bean 类似... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值