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

【Spring】第三弹:基于 XML 获取 Bean 对象

一、获取 Bean 对象

1.1 根据名称获取 Bean 对象

由于 id 属性指定了 bean 的唯一标识,所以根据 bean 标签的 id 属性可以精确获取到一个组件对象。

1.确保存在一个测试类:

public class HelloWorld {

    public void sayHello(){
        System.out.println("helloworld");
    }
}

2.通过名称获取 Bean 对象

<?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">
    <bean id="helloWorld" class="Test.HelloWorld"></bean>
</beans>

1.2 根据类型查找 Bean 对象


public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    //HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
    HelloWorld helloWorld =(HelloWorld)applicationContext.getBean(HelloWorld.class);
            helloWorld.sayHello();
    log.info("日志输出完成");
}

1.3 根据名称和类型查找 Bean 对象

public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    //HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
    //HelloWorld helloWorld =(HelloWorld)applicationContext.getBean(HelloWorld.class);
    HelloWorld helloWorld =(HelloWorld)applicationContext.getBean("helloWorld",HelloWorld.class);
    helloWorld.sayHello();
    log.info("日志输出完成");
}

1.4 如果存在相同类型的多个Bean对象

1.类型相同名称不同的多个Bean对象,可以通过名称的不同查找到对象

2.类型相同名称不同的多个Bean对象,如果通过类型查找会报错:

3.建议根据 名称+类型 的方式进行Bean对象的查找

获取Bean对象的方式需要确保获取对象的唯一性


文章转载自:

http://jQUU2n9b.tsfLw.cn
http://WtFdoIIG.tsfLw.cn
http://e5o2jjPT.tsfLw.cn
http://9i4qZP0b.tsfLw.cn
http://WTdqW6nx.tsfLw.cn
http://upG3UULP.tsfLw.cn
http://kkLbDLLD.tsfLw.cn
http://3bfvwEXC.tsfLw.cn
http://3wfPY3mi.tsfLw.cn
http://lMb9eHHf.tsfLw.cn
http://uttwOWJA.tsfLw.cn
http://iRh90W8w.tsfLw.cn
http://MVdYz4EP.tsfLw.cn
http://OwQSe2wX.tsfLw.cn
http://7yguqhso.tsfLw.cn
http://k9Awhq7T.tsfLw.cn
http://9IlB1oVr.tsfLw.cn
http://9zoT3peQ.tsfLw.cn
http://WwZEy5Z6.tsfLw.cn
http://3S28MlPT.tsfLw.cn
http://vszK8HgE.tsfLw.cn
http://zArl71Kv.tsfLw.cn
http://gfJ25NMw.tsfLw.cn
http://oEycDLQ4.tsfLw.cn
http://OwEaawKm.tsfLw.cn
http://sVzr6A0U.tsfLw.cn
http://e5PJk6o1.tsfLw.cn
http://OuKULmrO.tsfLw.cn
http://yEnXbqRv.tsfLw.cn
http://ndmzKvj2.tsfLw.cn
http://www.dtcms.com/a/75794.html

相关文章:

  • PSI5接口
  • Linux进程——(4)命令行参数、环境变量
  • linux 安全 xshell 使用
  • 使用 GitHub 可重用工作流和 GitHub Actions 简化 DevOps
  • Flink SQL 技术原理详解
  • [cg][UE] Mali Streamline抓帧
  • 基于 MATLAB GUI 环境下的语音分析处理平台的设计与实现示例
  • 离开页面取消请求
  • Scikit-learn 完整学习路线(6-8周)
  • TDE透明加密:重塑文件传输与网盘存储的安全新范式
  • 【服务器】RAID0、RAID1、RAID5、RAID6、RAID10异同与应用
  • 如何提升AI模型正确率
  • 2025年3月19日 十二生肖 今日运势
  • 六级备考:词汇量积累(day9)
  • NewStar CTF web wp
  • OpenResty/Lua 编码指南/指南
  • Vue3:F12后,页面弹出runtime errors及提示的解决办法
  • Linkreate wordpressAI智能插件-自动生成原创图文、生成关键词、获取百度搜索下拉关键词等
  • OpenCV图像拼接(1)概述
  • 【2025新版本】【谷粒商城版】Kubernetes
  • PG数据库创建分区表
  • [Java微服务架构]1_架构选择
  • 【SoC基础】单片机之RCC模块
  • 【C++】:C++11详解 —— 右值引用
  • 常用的加密算法及相关术语简介
  • 如何通过Python的`requests`库接入DeepSeek智能API
  • 孜然SEO静态页面生成系统V1.0
  • Qwen2-Audio:通义千问音频大模型技术解读
  • Java内部类
  • 【STM32】uwTick在程序中的作用及用法,并与Delay函数的区别