在Spring中我们经常使用@Autowired注解来将类的依赖注入进来,使用@Autowired注解有两个限制。第一:依赖对象在Spring容器中必须存在,否则将会抛出BeanCreationException异常;第二:依赖对象在Spring容器中只能存在一个;如果存在多个,则会抛出BeanCreationException异常;因为@Autowired注解是根据类型进行匹配的。
2016-1-17 21:44:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1908ca1: startup date [Sun Jan 17 21:44:54 CST 2016]; root of context hierarchy
2016-1-17 21:44:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2016-1-17 21:44:54 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14db0e3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,autowiredDao01,autowiredDao02,autowiredDao03,autowiredService01,autowiredService02,autowiredService03,autowiredService04,autowiredService05,autowiredService06,autowiredService07,autowiredService08,autowiredService09,qualifierService01,qualifierService02,qualifier01_demo1,qualifier01_demo2,qualifier01_demo3]; root of factory hierarchy
2016-1-17 21:44:55 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14db0e3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,autowiredDao01,autowiredDao02,autowiredDao03,autowiredService01,autowiredService02,autowiredService03,autowiredService04,autowiredService05,autowiredService06,autowiredService07,autowiredService08,autowiredService09,qualifierService01,qualifierService02,qualifier01_demo1,qualifier01_demo2,qualifier01_demo3]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qualifierService01': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ioc.qualifier.QualifierDao01 ioc.qualifier.QualifierService01.demoDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [ioc.qualifier.QualifierDao01] is defined: expected single matching bean but found 3: [qualifier01_demo1, qualifier01_demo2, qualifier01_demo3]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at ioc.qualifier.QualifierClient01.main(QualifierClient01.java:9)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ioc.qualifier.QualifierDao01 ioc.qualifier.QualifierService01.demoDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [ioc.qualifier.QualifierDao01] is defined: expected single matching bean but found 3: [qualifier01_demo1, qualifier01_demo2, qualifier01_demo3]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:280)
... 13 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [ioc.qualifier.QualifierDao01] is defined: expected single matching bean but found 3: [qualifier01_demo1, qualifier01_demo2, qualifier01_demo3]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:772)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:680)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:475)
... 15 more
从上面加粗字体可以看出,在Spring容器中QualifierDao01类存在三个实例,但是我们使用的是@Autowired,因此抛出异常。
解决办法:
使用@Qualifier注解来根据名称来指定你需要注入那个实例,如下:
@Autowired @Qualifier("qualifierDao01") private QualifierDao01 qualifierDao01;