2016-07-14 47 views
1

我正在学习斯普林斯,同时学习@Autowiring,碰到@Qualifier。我已经声明了一个限定符,但仍然有一个抛出的异常。斯普林斯资格赛错误

下面是我的代码

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" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


    <bean id="pointA" class="org.xyz.practice.Point"> 
     <qualifier value="myCircle" /> 
     <constructor-arg index="0" value="${pointA.pointX}"></constructor-arg> 
     <constructor-arg index="1" value="${pointA.pointY}"></constructor-arg> 
    </bean> 

    <bean id="pointB" class="org.xyz.practice.Point"> 
     <constructor-arg index="0" value="${pointA.pointX}"></constructor-arg> 
     <constructor-arg index="1" value="${pointA.pointY}"></constructor-arg> 
    </bean> 

    <bean 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations" value="configurations.properties"></property> 
    </bean> 



    <bean id="circle" class="org.xyz.practice.Circle"> 

    </bean> 

    <bean 
     class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 


</beans> 

Circle.java:

package org.xyz.practice; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 

public class Circle implements Shape { 

    private Point center; 

    public Point getCenter() { 
     return center; 
    } 

    @Autowired 
    @Qualifier("myCircle") 
    public void setCenter(Point center) { 
     this.center = center; 
    } 

    @Override 
    public void draw() { 
     System.out.println("Drawing a circle..."); 
     System.out.println("Circle point si (" + center.getX() + " , " + center.getY() + ")"); 
    } 

} 

MainClass:

package org.xyz.practice; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class DrawingApp { 

    public static void main(String[] args) { 

     ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
     Shape shape = (Shape) context.getBean("circle"); 
     shape.draw(); 
    } 

} 

例外:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'circle': Unsatisfied dependency expressed through method 'setCenter' parameter 0: No qualifying bean of type [org.xyz.practice.Point] is defined: expected single matching bean but found 2: pointA,pointB; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.xyz.practice.Point] is defined: expected single matching bean but found 2: pointA,pointB 
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'circle': Unsatisfied dependency expressed through method 'setCenter' parameter 0: No qualifying bean of type [org.xyz.practice.Point] is defined: expected single matching bean but found 2: pointA,pointB; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.xyz.practice.Point] is defined: expected single matching bean but found 2: pointA,pointB 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:647) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.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:775) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at org.xyz.practice.DrawingApp.main(DrawingApp.java:10) 
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.xyz.practice.Point] is defined: expected single matching bean but found 2: pointA,pointB 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:172) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1064) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) 
    ... 15 more 

请不要纪念这个为@Resource(相关的)问题重复,我想要去一步一步的。在您的参数

回答

1

完成Spring上下文通过XML通过向圆豆中心属性配置。

<bean id="circle" class="org.xyz.practice.Circle"> 
    <property name="center" class="package.Point"/> 
</bean> 

您还没有使用@annotation配置,尽量不要混合使用XML和@annotation配置。如果您想使用该配置,请将组件扫描添加到applicationContext.xml。

<context:component-scan base-package="base.package.to.scan" /> 
+0

嗨,朋友,这对我很好,但我提到的教程,这是没有提到,你可以让我知道发生了什么? – user3872094

+0

在这里,你明确地告诉Spring,你想要将哪两个存在点传递给你的'Circle'。使用XML。如果你不使用XML,但使用注释,那么你需要'Qualifier'来选择一个。或者你可以使用Spring的'@ Primary'注释。 – Nikem

+0

Spring提供了两种配置XML和@Annotation的方法。第一个,孔配置是用XML(s)文件(一个或多个)编写的;第二个使用'@Annotations'('@Autowire','@Qualifier','@Service',...)来配置bean和'组件扫描'来扫描代码中的bean。在你的代码中,你并没有启用'@Annotation',因为没有任何东西告诉Spring扫描包并寻找它们。 – Molinetas

0

认沽预选赛:

@Autowired 
public void setCenter(@Qualifier("myCircle") Point center) { 
    this.center = center; 
} 
+0

的'@是[指定] Qualifier'注释(http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Qualifier .html)能够注释一个字段,方法,参数(和其他)。为什么要把它放在参数上有什么不同?是什么让你这么想的? –

+0

@SotiriosDelimanolis我对Spring注入和'Qualifier'注解的体验。 – Nikem

+0

我想相信你的经验,但我需要更多。例如,如果您尝试通过您提出的更改来运行代码,则完全没有区别。这应该是你体验的一部分。这里的问题是他们已经注册了一个'AutowiredAnnotationBeanPostProcessor',但没有'QualifierAnnotationAutowireCandidateResolver'来处理'@ Qualifier'。 –