我正在學習斯普林斯,同時學習@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
(相關的)問題重複,我想要去一步一步的。在您的參數
嗨,朋友,這對我很好,但我提到的教程,這是沒有提到,你可以讓我知道發生了什麼? – user3872094
在這裏,你明確地告訴Spring,你想要將哪兩個存在點傳遞給你的'Circle'。使用XML。如果你不使用XML,但使用註釋,那麼你需要'Qualifier'來選擇一個。或者你可以使用Spring的'@ Primary'註釋。 – Nikem
Spring提供了兩種配置XML和@Annotation的方法。第一個,孔配置是用XML(s)文件(一個或多個)編寫的;第二個使用'@Annotations'('@Autowire','@Qualifier','@Service',...)來配置bean和'組件掃描'來掃描代碼中的bean。在你的代碼中,你並沒有啓用'@Annotation',因爲沒有任何東西告訴Spring掃描包並尋找它們。 – Molinetas