[英]Difference between @Primary vs @Autowired with @Qualifier annotations
[英]Autowired and Qualifier java annotations
我對此注釋有疑問。
這是我的Circle課程:
public class Circle implements Shape {
private Point center;
public Point getCenter() {
return center;
}
@Autowired
@Qualifier("circleRelated")
public void setCenter(Point center) {
this.center = center;
}
@Override
public void draw() {
System.out.println("Drawing cicrle " + center.getX() + ", " + center.getY());
}
}
這是我的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.xsd">
<bean id="pointA" class="com.majewski.javabrains.Point">
<qualifier value="circleRelated" />
<property name="x" value ="0" />
<property name="y" value ="0" />
</bean>
<bean id="pointB" class="com.majewski.javabrains.Point">
<property name="x" value ="0" />
<property name="y" value ="1" />
</bean>
<bean id="pointC" class="com.majewski.javabrains.Point">
<property name="x" value ="0" />
<property name="y" value ="20" />
</bean>
<bean id="circle" class="com.majewski.javabrains.Circle" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
</beans>
這是我的主要方法:
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();
Shape shape = (Shape) context.getBean("circle");
shape.draw();
}
我收到一個錯誤:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circle': Injection of autowired dependencies
failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method:
public void com.majewski.javabrains.Circle.setCenter(com.majewski.javabrains.Point);
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.majewski.javabrains.Point] is defined:
expected single matching bean but found 3: pointA,pointB,pointC
我已經在“ pointA” bean中添加了選項卡,所以我不明白我的程序出了什么問題。 有人可以向我解釋嗎?
我想你不見了
<context:annotation-config />
因此,在您的Spring上下文中,XML和@Qualifier("circleRelated")
被忽略。
-編輯-
當然,為了使用它,您需要擴展名稱空間定義...
<beans
...
xmlns:context="http://www.springframework.org/schema/context"
...
xsi:schemaLocation=".... http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-YOUR-SPRING-VERSION-HERE.xsd">
很好,問題出在限定符@Qualifier("circleRelated")
,因為您嘗試綁定名稱為circleRelated的bean,但是在spring-conf.xml中,您僅聲明了3個名稱為pointA,pointB,pointC的點,因此無法匹配並注入實例,因此可以將@Qualifier("circleRelated")
更改為@Qualifier("circleRelated")
@Qualifier("pointA")
或使用<bean id="circleRelated" class="com.majewski.javabrains.Point">
在xml中聲明一個bean <bean id="circleRelated" class="com.majewski.javabrains.Point">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.