用spring的时候出现了多个bean该怎么确定我想要的那个bean

本文探讨了Spring框架中处理多个相同类型Bean的装配策略,通过使用@Primary注解和指定Bean名称来解决歧义性问题,确保正确装配所需的服务实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载 :

Spring中Bean装配的歧义性

本文链接:https://blog.youkuaiyun.com/u010502101/article/details/76563699

在spring容器中有多个同类的Bean时,该如何装载呢?例如有一个服务接口A,该接口有3个实现类,在容器中就会生成3个A的实现类的Bean,当对A进行装载时,容器不会判断装载哪一个,就会报一个没有唯一的一个Bean。要想解决此问题,最简单的方法就是用Primary注解,Primary注解表示优先装载Bean。但是最方便的方法是指定装载Bean的名称。下面用代码演示

一、优先装载

1、服务接口

package com.spring.primary.annotation;

public interface Animal {

    void eat();

}

2、接口的实现类
总共有猪、狗、猫实现接口
猪:

package com.spring.primary.annotation;

import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;

@Component@Primary //当容器中有多个Bean同类的Bean时,优先注入该Beanpublic class Pig implements Animal {

 

    @Override

    public void eat() {

        System.out.println("我是猪,我吃猪食");

    }

}

猫:

package com.spring.primary.annotation;

import org.springframework.stereotype.Component;

@Componentpublic class Cat implements Animal {

 

    @Override

    public void eat() {

        System.out.println("我是猫,我吃猫食");

    }

}

狗:

package com.spring.primary.annotation;

import org.springframework.stereotype.Component;

@Componentpublic class Dog implements Animal {

 

    @Override

    public void eat() {

        System.out.println("我是狗,我吃狗食");

    }

}

3、配置类
新建一个配置类,用于配置要装载的Bean。

package com.spring.primary.annotation;

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;

@Configuration@ComponentScan//默认扫描所在的包public class AnnotationConfig {

 

    private Animal animal;

 

    @Autowired

    //容器中有三个关于Animal的Bean,优先注入猪的那个Bean

    public void setAnimal(Animal animal) {

        this.animal = animal;

    }

}

4、测试方法如下

package com.spring.primary.annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMain {

 

    public static void main(String[] args) {

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationConfig.class);

        Animal animal = ctx.getBean(Animal.class);

        //此处获取到的Bean是猪的Bean

        animal.eat();

    }

}

5、输出结果如下

我是猪,我吃猪食

6、总结
从上面实例可以看出,注解Primary一定程度上避免了容器中有多个同类型的Bean时,不知道注入哪个Bean的程序异常。但是这不是最好的方法,下面用限定名装载Bean。

二、限定名装载Bean

1、服务接口不变,实现类分别为:

package com.spring.primary.annotation;

import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;

@Component("pigIml")public class Pig implements Animal {

    @Override

    public void eat() {

        System.out.println("我是猪,我吃猪食");

    }

}

狗和猫的限定名分别改为:
@Component(“dogIml”)和@Component(“catIml”)

2、配置类

package com.spring.primary.annotation;

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

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration

@ComponentScan

//默认扫描所在的包

public class AnnotationConfig {

}

3、测试类

package com.spring.primary.annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMain {

 

    public static void main(String[] args) {

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationConfig.class);

        Animal animal = (Animal) ctx.getBean("pigIml");

        animal.eat();

    }

}

4、输出结果为

我是猪,我吃猪食

因此从上面对比可以看出,优先用限定名的Bean去装载,更容易。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值