2016-07-14 70 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'。 –