Xml 为什么Spring basic@Autowired不工作?
春天我是新来的。但是尝试基本的东西。根据理论,我应该工作,但它不工作。我有两个id不同的相同bean。我应该能够@Autowired with unique id@QualifierPersonBean2。但它给出了错误。我不知道哪里出了什么问题。请通知我好吗?谢谢 其中是config springBeans.xml:-Xml 为什么Spring basic@Autowired不工作?,xml,spring,autowired,Xml,Spring,Autowired,春天我是新来的。但是尝试基本的东西。根据理论,我应该工作,但它不工作。我有两个id不同的相同bean。我应该能够@Autowired with unique id@QualifierPersonBean2。但它给出了错误。我不知道哪里出了什么问题。请通知我好吗?谢谢 其中是config springBeans.xml:- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w
<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-2.5.xsd">
<bean
class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"/>
<bean id="customer" class="com.mkyong.common.Customer" >
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean1" class="com.mkyong.common.Person">
<property name="name" value="mkyong1" />
<property name="address" value="address 1" />
<property name="age" value="28" />
</bean>
<bean id="PersonBean2" class="com.mkyong.common.Person">
<property name="name" value="mkyong2" />
<property name="address" value="address 2" />
<property name="age" value="28" />
</bean>
</beans>
Person.java:-
public class Person {
private String name;
private String address;
private int age;
}
所有的塞特和盖特都被移除了
Test app.java:-
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"SpringBeans.xml");
Customer cust = (Customer) context.getBean("customer");
System.out.println(cust);
}
}
错误:-
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customer': Autowiring of fields failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mkyong.common.Person com.mkyong.common.Customer.person;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mkyong.common.Person] is defined: expected single matching bean but found 2: [PersonBean1, PersonBean2]
它应该有用!@Qualifier注释似乎没有按预期工作 仔细检查@Qualifier注释是否从包org.springframework.beans.factory.annotation导入
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
另一个技巧是,与版本化架构spring-beans-2.5.xsd相比,您更喜欢无版本架构spring-beans.xsd。确保使用了正确的@Qualifier,即spring中的@Qualifier,而不是javax.injectOne。是的,请导入org.springframework.beans.factory.annotation.Qualifier;我认为它是正确的。您添加了AutowiredAnnotationBeanPostProcessor,顾名思义,它只处理@Autowired注释。不是直接使用处理器,而是添加其他处理器,这些处理器还考虑@Qualifier。是从org.springframework.beans.factory.annotation.Qualifier导入的
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />