org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.NoUniqueBeanDefinitionException

问题描述 投票:1回答:1

如何解决Spring中Bean的AUTOMATIC连接中的歧义?我们有一个Dessert接口,我们有三个不同的甜点(Beans)来实现接口(甜点)。

当天的甜点是Cookie,因此我们将它写成@Primary的最爱。

问题是甜点已经做好了,如果有人想重复甜点,就不会有饼干,所以我们为那些想要重复甜点的人创造了“重复甜点”。

除了最喜欢的甜点外,还有两种甜点,CAKE和ICECREAM。我们希望每个重复的人都能享用冰淇淋甜点,我们已经足够了。

我们如何告诉Spring我们想要它服务的两种甜点中的哪一种?

public interface Dessert {
    void eat();
}

豆蛋糕:

@Component
public class Cake implements Dessert{

    private Dessert repeatDessert;

    public Dessert getRepeatDessert() {
        return repeatDessert;
    }

    @Autowired
    public void setRepeatDessert(Dessert repeatDessert) {
       this.repeatDessert = repeatDessert;
    }

    @Override
    public void eat() {
        System.out.println("Eating a Cake !!!!");
    }
}

BEAN COOKIE:

@Component
@Primary
public class Cookie implements Dessert{

    private Dessert repeatDessert;

    public Dessert getRepeatDessert() {
        return repeatDessert;
    }

    @Autowired
    public void setRepeatDessert(Dessert repeatDessert) {
       this.repeatDessert = repeatDessert;
    }

    @Override
    public void eat() {
        System.out.println("Eating a Cookie !!!!");
    }
}

BEAN ICECREAM:

@Component
public class IceCream implements Dessert{

    private Dessert repeatDessert;

    public Dessert getRepeatDessert() {
        return repeatDessert;
    }

    @Autowired
    public void setRepeatDessert(Dessert repeatDessert) {
       this.repeatDessert = repeatDessert;
    }

    @Override
    public void eat() {
        System.out.println("Eating a IceCream !!!!");
    }
}

这将是配置文件:

@Configuration
@ComponentScan
public class AutoBeanConfiguration {

}

这将是主类:

public class Main {

    public static void main(String[] args) {
        BasicConfigurator.configure();
        Logger.getRootLogger().setLevel(Level.ERROR);
        AnnotationConfigApplicationContext ctxt = new AnnotationConfigApplicationContext(AutoBeanConfiguration.class);
        Cookies cookies = ctxt.getBean(Cookies.class);
        cookies.eat();
        Dessert dessert = cookies.getRepeatDessert();
        dessert.eat();
        ctxt.close();
    }

}

该程序抛出以下exception

Exception in thread "main" Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cookies': Unsatisfied dependency expressed through method 'setRepeatDessert' parameter 0;
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'Dessert' available: expected single matching bean but found 2: cake,iceCream

当我们想要重复时,我们如何解决模糊性并告诉我们想要创造哪些甜点?

控制台输出应为:

Eating a Cookies !!!!
Eating a IceCream !!!!
java spring javabeans
1个回答
2
投票

首先,您的代码有编译错误。请更改此代码

Cookies cookies = ctxt.getBean(Cookies.class);

Cookie cookie = ctxt.getBean(Cookie.class);

然后在bean类中,所有setter方法都与repeatDessert同名。因此,这会找到与此名称相同的所有repeatDessert bean。所以这找不到任何带有this的bean。在你的bean的setter方法中;

@Autowired
public void setRepeatDessert(Dessert repeatDessert) {
   this.repeatDessert = repeatDessert;
}

方法参数repeatDessert未指定哪个bean。如果你用Qualifier改变这样的所有bean类。 Read more detail about Qualifier;

对于Cake.class;

@Autowired
@Qualifier("cake")
public void setRepeatDessert(Dessert repeatDessert) {
    this.repeatDessert = repeatDessert;
}

对于IceCream.class;

@Autowired
@Qualifier("iceCream")
public void setRepeatDessert(Dessert repeatDessert) {
    this.repeatDessert = repeatDessert;
}

对于Cake.class;

@Autowired
@Qualifier("cake")
public void setRepeatDessert(Dessert repeatDessert) {
    this.repeatDessert = repeatDessert;
}

现在你的春季项目有效。但我认为你的结果无法产生。因为你的主要没有关于冰淇淋的任何信息,只有你有Cookie但你想打印Eating a IceCream !!!!。我从你的输出中不明白。如果这不能帮你解决问题,你能提供吗?

PS:如果你想改变你班级的repeatDessert,你可以改变@Qualifier("yourBeanName");,你可以提供任何可重复的沙漠。

© www.soinside.com 2019 - 2024. All rights reserved.