나는 스프링스를 배우고 @Autowiring
을 배우는 동안 @Qualifier
을 발견했습니다. 나는 한정어를 선언했지만 여전히 예외가 발생합니다.스프링 한정자 오류
<?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 :
,369 아래 는Spring.xml 내 코드입니다
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
여기서 스프링을 명시 적으로 말하면, 2 개의 기존 점 중 어느 것이 당신의 'Circle'에 전달되기를 원하십니까? XML 사용. XML을 사용하지 않고 주석을 사용한다면, 어떻게 든 선택하기 위해 '한정어'가 필요합니다. 또는 Spring의 @ @ Primary 주석을 사용할 수 있습니다. – Nikem
Spring은 XML과 @Annotation을 구성하는 두 가지 방법을 제공합니다. 첫 번째 홀 구성은 XML 파일 (하나 이상)로 작성됩니다. 두 번째로는 '@Annotations'('@Autowire', '@Qualifier', '@Service'등)를 사용하여 Bean을 구성하고 'Component-Scan'을 사용하여 코드에서 Bean을 검색합니다. 귀하의 코드에서 '@Annotation'을 활성화하지는 않습니다. 왜냐하면 Spring이 패키지를 검색하여 찾아 보도록하기 때문에. – Molinetas