当前位置: 首页 > web >正文

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

Spring中IOC容器中获取组件Bean

实体类

//接口
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 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

    /*** 在IOC容器中获取组件Bean*/@Testpublic 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); // trueSystem.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...}
http://www.xdnf.cn/news/7087.html

相关文章:

  • 计算机科技笔记: 容错计算机设计05 n模冗余系统 TMR 三模冗余系统
  • 【25软考网工】第六章(7)网络安全防护系统
  • 入门OpenTelemetry——应用自动埋点
  • 20242817-李臻-课下测试:基于商用密码的数字信封协议(AI)
  • 基于 STM32 的手持式安检金属探测器设计与实现
  • AI大模型学习二十六、使用 Dify + awesome-digital-human-live2d + ollama + ChatTTS打造数字人
  • 图绘Linux:基础指令脉络阁
  • 学习黑客Active Directory 入门指南(二)
  • C语言:在 Win 10 上,gcc 如何编译 调用 Tcl/Tk 的C程序
  • Jmeter使用及压测
  • Linux下 使用 SSH 完成 Git 绑定 GitHub
  • 【Linux】ELF与动静态库的“暗黑兵法”:程序是如何跑起来的?
  • 什么是迁移学习(Transfer Learning)?
  • .NET外挂系列:1. harmony 基本原理和骨架分析
  • GitHub 趋势日报 (2025年05月17日)
  • 【C++】unordered_map与set的模拟实现
  • 45 python csv(存储表格数据)
  • Day28 Python打卡训练营
  • 赋能企业级移动应用 CFCA FIDO+提升安全与体验
  • 题单:递归求和
  • 上集:一个前端的血泪复仇记 —— 静态部署的胜利
  • java每日精进 5.15【分页实现】
  • C语言斐波那契数列
  • 日期数据渲染转换问题
  • 深度学习推理引擎---OpenVINO
  • SEO长尾词与关键词优化实战
  • Python语法强化
  • Python实现NOA星雀优化算法优化卷积神经网络CNN回归模型项目实战
  • 2025最新软件测试面试题(含答案解析+文档)
  • 【C语言练习】047. 理解递归与循环的转换