gpt4 book ai didi

java - 使用 Qualifier 注入(inject) Autowiring 的依赖项失败

转载 作者:数据小太阳 更新时间:2023-10-29 01:52:25 25 4
gpt4 key购买 nike

我已经尝试了所有可以在任何地方找到的选项,我查看了之前针对该主题提出的所有问题并尝试了其中给出的解决方案,但没有任何效果,我得到的只是这个错误。

错误

INFO: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" 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 spring.springdemo.Circle.setCenter(spring.springdemo.Point); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.springdemo.Point] is defined: expected single matching bean but found 3: pointA,pointB,pointC
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at spring.springdemo.DrawingApp.main(DrawingApp.java:10)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void spring.springdemo.Circle.setCenter(spring.springdemo.Point); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.springdemo.Point] is defined: expected single matching bean but found 3: pointA,pointB,pointC
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 13 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.springdemo.Point] is defined: expected single matching bean but found 3: pointA,pointB,pointC
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:553)
... 15 more

我在这个例子中创建的文件如下。

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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

这是我想要应用 Autowiring 的地方。

 <bean id="circle" class="spring.springdemo.Circle">

</bean>

<bean id="pointA" class="spring.springdemo.Point">
<qualifier value="CircleRelated"/>
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>
<bean id="pointB" class="spring.springdemo.Point">
<property name="x" value="-20"/>
<property name="y" value="0"/>
</bean>
<bean id="pointC" class="spring.springdemo.Point">
<property name="x" value="20"/>
<property name="y" value="0"/>
</bean>

这些是我添加的类。

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

Circle.java类,即对象将由 Autowiring 填充

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


public class Circle implements Shape{
private Point center;

@Override
public void draw() {
System.out.println("Drawing Circle");
System.out.println("Point is : ("+center.getX()+", "+center.getY()+")");
}
public Point getCenter() {
return center;
}
@Autowired
@Qualifier("CircleRelated")
public void setCenter(Point center) {
this.center = center;
}

}

最佳答案

我建议您删除 <qualifier value="CircleRelated"/>从 xml 中直接从 id 中注入(inject)依赖项。 xml中的id在大​​多数情况下就足够了,限定符只能用于特定情况。并且惯例是使用小写作为 id 或限定符的首字母。

所以,改变CircleRelated@Qualifier("CircleRelated")通过现有 ID,如 @Qualifier("pointA") , @Qualifier("pointB")@Qualifier("pointC")

关于java - 使用 Qualifier 注入(inject) Autowiring 的依赖项失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22302388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com