自动连线和限定符java注释

自动连线和限定符java注释,java,spring,Java,Spring,我对此注释有问题 这是我的圆圈课: public class Circle implements Shape { private Point center; public Point getCenter() { return center; } @Autowired @Qualifier("circleRelated") public void setCenter(Point center) { this.ce

我对此注释有问题

这是我的圆圈课:

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>
我得到一个错误:

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  />

我想你失踪了

<context:annotation-config  />


问题出在限定符
@qualifier(“CirclerRelated”)
中,因为您试图用名称CirclerRelated绑定一个bean,但在spring-conf.xml中,您只声明了3个名为pointA、pointB、pointC的点,因此它无法匹配并注入实例,所以要么更改
@qualifier(“CirclerRelated”)
@Qualifier(“pointA”)
或用
在xml中声明一个bean,问题出在Qualifier
@Qualifier(“circlerrelated”)
中,因为您试图用名称circlerrelated绑定一个bean,但在spring-conf.xml中,您仅用名称声明了3个点:pointA、pointB、pointC、,因此它无法匹配并注入实例,因此可以将
@Qualifier(“CirclerRelated”)
更改为
@Qualifier(“pointA”)
,或者使用

在xml中声明一个bean。您确定包名吗?尝试在Cicle限定符注释中使用bean id(pointA)它会给出与“pointA”相同的错误在限定符中,当我注释掉pointB和PointCy时,它可以正常工作。您没有名为
“CirclerRelated”
的bean。只有beans
pointA
pointB
pointC
@Stewart我不想使用这个名称,所以我正在使用。您确定包名吗?尝试在Cicle限定符注释中使用bean id(pointA)它给出了与“pointA”相同的错误在限定符中,当我注释掉pointB和PointCy时,它可以正常工作。您没有名为
“CirclerRelated”
的bean。只有beans
pointA
pointB
pointC
@Stewart我不想使用这个名称,所以我现在使用的是我得到了一个错误:“元素“context:annotation config”的前缀“context”没有绑定。”@krzysztofmayewski编辑了答案。。。您需要使用“xmlns”和“xsi:schemaLocation”声明“context”命名空间…这是我的新.xml头:我收到一个错误,无法找到这些位置。读取架构文档“”失败,因为1)找不到该文档;2) 文件无法读取;3) 文档的根元素不是。天哪,我太笨了。没有注意到您的xD版本,很抱歉,现在我收到了这个错误:“元素“context:annotation config”的前缀“context”没有绑定。”@krzysztofmayewski我编辑了答案。。。您需要使用“xmlns”和“xsi:schemaLocation”声明“context”命名空间…这是我的新.xml头:我收到一个错误,无法找到这些位置。读取架构文档“”失败,因为1)找不到该文档;2) 文件无法读取;3) 文档的根元素不是。天哪,我太笨了。没有注意到您的xD版本Sorry@AntJavaDev可能不是,因为应该被注入的Bean在xml中使用。。。可能是由于未设置Springs注释配置,所以忽略了限定符注释。@AndréR.,我明白你的意思,你是对的,他缺少了,否则他必须管理xmlKrzysztofMajewski中的连接您的@Autowired注释被忽略,因为在spring-conf.xml中,您缺少了AndréR的答案中的标记,所以正确的配置应该是@AntJavaDev之类的,可能不是,因为应该注入的Bean在xml中使用。。。可能是由于未设置Springs注释配置,所以忽略了限定符注释。@AndréR.,我明白你的意思,你是对的,他缺少了,否则他必须管理xmlKrzysztofMajewski中的连接您的@Autowired注释被忽略,因为在spring-conf.xml中,您缺少了安德烈回答中的标记,所以正确的配置如下
<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">