2_Spring【IOC容器中获取组件Bean】

Spring中IOC容器中获取组件Bean

实体类

java 复制代码
//接口
public interface TestDemo {
    public void doSomething();
}
// 实现类
public class HappyComponent implements TestDemo {
    public void doSomething() {
        System.out.println("HappyComponent is doing something...");
    }
}

创建Bean配置文件

spring-03.xml

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    组件信息做ioc配置 ->applicationContext读取->实例化对象-->
    <bean id="happyComponent" class="com.atguigu.ioc_03.HappyComponent"/>
</beans>

IOC容器中获取组件Bean

java 复制代码
    /**
     * 在IOC容器中获取组件Bean
     */
    @Test
    public void getBeanFormIocTest() {
//        1.创建IOC容器
        ClassPathXmlApplicationContext applicationContext1 = new ClassPathXmlApplicationContext();
        applicationContext1.setConfigLocations("spring-03.xml"); // 外部配置文件设置
        applicationContext1.refresh(); // 刷新容器 IOC、DI的动作
//        2.读取IOC容器的组件
//        方式一:直接根据BeanId获取 获取的是一个Object对象 需要强制转换[不推荐]
        HappyComponent happyComponent = (HappyComponent)applicationContext1.getBean("happyComponent");
//        方式二:根据BeanId和类型获取 获取的是一个指定类型的对象[推荐]
        HappyComponent happyComponent1 = applicationContext1.getBean("happyComponent", HappyComponent.class);
//        方式三:根据类型获取 获取的是一个指定类型的对象
//        TODO 根据bean的类型获取,同一个类型,在ioc容器中只能有一个bean!!!(如果ioc容器存在多个同类型的bean,会出现NoUniqueBeanDefinitionException[不唯一]异常)
        HappyComponent happyComponent2 = applicationContext1.getBean(HappyComponent.class);
//        3.使用组件
        happyComponent.doSomething();
        happyComponent1.doSomething();
        happyComponent2.doSomething();
        System.out.println(happyComponent == happyComponent1); // true
        System.out.println(happyComponent == happyComponent2); // true
//        4.IOC的bean配置一定是实现类,但是可以根据接口类型获取值
//        getBean(,类型) 其中的类型判断采用  instanceof ioc容器的类型 == true  (接口与实现类之间是true的关系)
//        在实际业界使用这种方式,可以将部分逻辑抽离出来,封装工具,只留接口作为对外暴露的api,继承自该接口的直接实现类可以直接使用
        TestDemo testDemo = applicationContext1.getBean("happyComponent", TestDemo.class);
        TestDemo testDemo1 = applicationContext1.getBean(TestDemo.class); // 这样更简洁但是要注意 bean中同一类型只能有一个!!!
        testDemo.doSomething(); // HappyComponent is doing something...
    }
相关推荐
考虑考虑6 小时前
JDK27结构化并发
java·后端·java ee
Cosolar9 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
会编程的吕洞宾18 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤18 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha18 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
RobinXYuan19 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读
炸薯条!19 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
SimonKing20 小时前
又一款数据库同步神器,2.3K Star的dbswitch
java·后端·程序员
CoderYanger20 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
Rain的Java大神之路20 小时前
前端转 Java 全栈:用 AI 两天交付一个能跑的 Demo,踩坑全记录
java·前端·后端