IOC - 从容器获取 bean

从 IOC 容器获取 bean

  • 有 3 种获取方式

    1. 根据 bean 名称,只能获取一个
    2. 根据 bean 类型,可以获取一个,也可以获取多个
    3. 根据 bean 名称+类型,只能获取一个,期望精准获取
  • 获取的 bean 也有三种情况

    1. bean 不存在

      不管哪种获取方式,结果都是一样的(因为 bean 不存在),会抛出异常 NoSuchBeanDefinitionException

    2. bean 存在,且唯一

      不管哪种方式获取,结果也是一样的(因为只有一个 bean),能正常获取到

    3. bean 存在,但不唯一

      1. 按照 bean 名称获取,名称是唯一的,能正常获取,就算有同名的 bean 重复注册,IOC 容器也只会保存最先创建的 bean
      2. 按照 bean 类型获取,如果获取一个,会抛出异常:NoUniqueBeanDefinitionException
      3. 按照 bean 类型获取,获取多个,能正常获取,返回所有的 bean,返回一个 Map
      4. 按照 名称 + 类型 获取,能正常获取
package com.cyrus.springioc;

import com.cyrus.springioc.bean.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Map;

@SpringBootApplication
public class SpringIocApplication {

    public static void main(String[] args) {

        // 获取 ioc 容器
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIocApplication.class, args);

        // 所有的 bean(输出 bean 名称)
        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }

        // bean 不存在:NoSuchBeanDefinitionException
        Person zhangsan = (Person) ioc.getBean("zhangsan");

        // bean 名称重复:获取到最先注册的 bean(其实是不能重复注册,后面注册的不会覆盖前面的)
        Person person = (Person) ioc.getBean("person");
        System.out.println(person);

        // 某个种类型的 bean 是多个,可以全部获取,返回 Map(key 是 neam 名称,value 是 bean 对象)
        Map<String, Person> beansOfType = ioc.getBeansOfType(Person.class);
        System.out.println(beansOfType);

        // 某种类型的 bean 是多个,根据类型只获取一个:NoUniqueBeanDefinitionException
        Person bean = ioc.getBean(Person.class);

        // 某种类型的 bean 是多个,根据名称只获取一个:精准获取
        Person person3 = (Person) ioc.getBean("person3");
        System.out.println(person3);

        // 根据名称+类型来精确获取 bean
        Person person4 = ioc.getBean("person4", Person.class);
        System.out.println(person4);

    }

    @Bean("person")
    public Person person1() {
        Person person = new Person();
        person.setAge(18);
        person.setName("Marry");
        return person;
    }

    @Bean("person")
    public Person person2() {
        Person person = new Person();
        person.setAge(22);
        person.setName("July");
        return person;
    }

    @Bean
    public Person person3() {
        Person person = new Person();
        person.setAge(24);
        person.setName("Bob");
        return person;
    }

    @Bean
    public Person person4() {
        Person person = new Person();
        person.setAge(26);
        person.setName("Milk");
        return person;
    }

}
posted @   CyrusHuang  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示