我正在研究@Autowired()
和@Qualifer()
以測試對依賴項的自動注入。我創建了引擎類,汽車類。引擎類包含兩個對象e1和e2。當我使用@Qualifier
具有值 「E2」,則生成錯誤消息:@Qualifier()和@Autowired()不起作用
WARNING: Exception encountered during context initialization -
cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'c': Injection of autowired dependencies failed; nested
exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.abc.xyz.Engine
com.abc.xyz.Car.engine; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.abc.xyz.Engine] is defined: expected single
matching bean but found 2: e1,e2
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'c': Injection of autowired dependencies failed; nested
exception is org.springframework.beans.factory.BeanCreationException: Could
not autowire field: private com.abc.xyz.Engine com.abc.xyz.Car.engine;
nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.abc.xyz.Engine] is defined: expected single
matching bean but found 2: e1,e2
at
org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor
.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.
bstractAutowireCapableBeanFactory.populateBean
(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.doCreateBean
(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.createBean
(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.
AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.
DefaultSingletonBeanRegistry.getSingleton
(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.
AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.
AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.
DefaultListableBeanFactory.preInstantiateSingletons
(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.
AbstractApplicationContext.finishBeanFactoryInitialization
(AbstractApplicationContext.java:839)
at org.springframework.context.support.
AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.
ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.
ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:83)
at com.abc.xyz.ClientApp.main(ClientApp.java:9)
Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.abc.xyz.Engine
com.abc.xyz.Car.engine; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.abc.xyz.Engine] is defined: expected single
matching bean but found 2: e1,e2
at org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject
(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject
(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues
(AutowiredAnnotationBeanPostProcessor.java:331)
... 13 more
Caused by: org.springframework.beans.factory.
NoUniqueBeanDefinitionException: No qualifying bean of type
[com.abc.xyz.Engine] is defined: expected single matching
bean but found 2: e1,e2
at
org.springframework.beans.factory.
support.DefaultListableBeanFactory.doResolveDependency
(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.
support.DefaultListableBeanFactory.resolveDependency
(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.
annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.
inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 15 more
用於上述的代碼是:
Engine.java:
package com.abc.xyz;
public class Engine {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Car.java :
package com.abc.xyz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Car {
@Qualifier(value="e1")
@Autowired()
private Engine engine;
public void display()
{
System.out.println("Car Engine="+engine.getName());
}
}
spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<beanclass="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor">
</bean>
<bean id="e1" class="com.abc.xyz.Engine">
<property name="name" value="2015"></property>
</bean>
<bean id="e2" class="com.abc.xyz.Engine">
<property name="name" value="2016"></property>
</bean>
<bean id="c" class="com.abc.xyz.Car">
</bean>
</beans>
ClientApp.java:
package com.abc.xyz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext
("spring.xml");
Car c=(Car)context.getBean("c");
c.display();
}
}
嘗試在限定符前放置自動裝配。或更好的,使用@Resource(「e1」) – Sarief
@Sarief我試過但沒有改變輸出 –
你使用的是maven嗎?你能不能發佈你的pom.xml呢? –