主要内容

添加BeanReference类,包装一个bean对另一个bean的引用。如beanA引用beanB,那么在实例化beanA时,如果propertyValue.value是BeanReference类型,引用beanB,那么先实例化beanB。在这里为了便于大家理解暂时不引入三级缓存解决循环依赖,三级缓存会在后面高级篇单独讲解。

代码分支

populate-bean-with-bean

核心代码

BeanReference

public class BeanReference {
private String name; public BeanReference(String name) {
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

AbstractAutowireCapableBeanFactory.applyPropertyValues方法

    private void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
for(PropertyValue propertyValue : beanDefinition.getPropertyValues().getPropertyValues()){
String name = propertyValue.getName();
Object value = propertyValue.getValue();
if(value instanceof BeanReference){
BeanReference beanReference = (BeanReference)value;
value = getBean(beanReference.getName());
}
BeanUtil.setProperty(bean,name,value);
}
}

测试

@Test
public void testBeanWithBean(){
PropertyValues propertyValuesForPerson = new PropertyValues();
propertyValuesForPerson.addPropertyValue(new PropertyValue("name","yiHui"));
propertyValuesForPerson.addPropertyValue(new PropertyValue("age",20));
propertyValuesForPerson.addPropertyValue(new PropertyValue("car",new BeanReference("car"))); PropertyValues propertyValuesForCar = new PropertyValues();
propertyValuesForCar.addPropertyValue(new PropertyValue("name","Rolls-Royce")); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition("car",new BeanDefinition(Car.class,propertyValuesForCar));
factory.registerBeanDefinition("person",new BeanDefinition(Person.class,propertyValuesForPerson)); Person person = (Person)factory.getBean("person");
System.out.println(person);
}

测试结果

Person{name='yiHui', age='20', car=Car{name='Rolls-Royce'}}

[源码系列:手写spring] IOC第五节:Bean注入Bean的更多相关文章

  1. Spring源码分析 手写简单IOC容器

    Spring的两大特性就是IOC和AOP. IOC Container,控制反转容器,通过读取配置文件或注解,将对象封装成Bean存入IOC容器待用,程序需要时再从容器中取,实现控制权由程序员向程序的 ...

  2. Spring IOC(五)依赖注入

    Spring IOC(五)依赖注入 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.autowire 五种注入方式测试 ...

  3. 《四 spring源码》手写springioc框架

    手写SpringIOCXML版本 /** * 手写Spring专题 XML方式注入bean * * * */ public class ClassPathXmlApplicationContext { ...

  4. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

  5. 从零开始手写 spring ioc 框架,深入学习 spring 源码

    IoC Ioc 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理. 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过 ...

  6. 框架源码系列六:Spring源码学习之Spring IOC源码学习

    Spring 源码学习过程: 一.搞明白IOC能做什么,是怎么做的  1. 搞明白IOC能做什么? IOC是用为用户创建.管理实例对象的.用户需要实例对象时只需要向IOC容器获取就行了,不用自己去创建 ...

  7. 《四 spring源码》手写springmvc

    手写SpringMVC思路 1.web.xml加载  为了读取web.xml中的配置,我们用到ServletConfig这个类,它代表当前Servlet在web.xml中的配置信息.通过web.xml ...

  8. Spring源码 20 手写模拟源码

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  9. 利用递归,反射,注解等,手写Spring Ioc和Di 底层(分分钟喷倒面试官)了解一下

    再我们现在项目中Spring框架是目前各大公司必不可少的技术,而大家都知道去怎么使用Spring ,但是有很多人都不知道SpringIoc底层是如何工作的,而一个开发人员知道他的源码,底层工作原理,对 ...

  10. Spring源码剖析3:Spring IOC容器的加载过程

    本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...

随机推荐

  1. JuiceFS 2024:开源与商业并进,迈向 AI 原生时代

    即将过去的 2024 年,是 JuiceFS 开源版本推出的第 4 年,企业版的第 8 个年头.回顾过去这一年,JuiceFS 社区版依旧保持着快速成长的势头,GitHub 星标突破 11.1K,各项 ...

  2. 280:监控和管理LINUX进程

  3. Hbuilder使用快捷键

    Hbuilder的使用 1.Hbuilder基本操作​设置基本外观文字大小,申请账号.​2.Hbuilder快捷键​- 新建菜单: ctrl + N​- 新建: ctrl + N​- 关闭: ctrl ...

  4. 动手学大模型应用开发,第4天:Prompt设计

    第一章.Prompt 设计的原则和技巧 LLM 时代 prompt 这个词对于每个使用者和开发者来说已经听得滚瓜烂熟,那么到底什么是 prompt 呢?简单来说,prompt(提示) 就是用户与大模型 ...

  5. Docker 镜像存储目录的位置修改教程

    以下是在 Linux 系统中修改 Docker 镜像存储目录位置的一般步骤: 查看当前 Docker 的默认存储目录:使用docker info命令可以查看 Docker 存储驱动程序和默认存储位置, ...

  6. Hive - 多种表类型的CURD测试

    关于torc.textfile.orc.es.hyperdrive表的CURD测试 TORC(支持事务的orc表)测试 TORC(分区表)测试 TEXTFILE 表测试 ORC 表测试 ES(Elas ...

  7. 试试使用 Vitest 进行测试,确实可以减少bug

    vitest的简单介绍 Vitest 是一个基于 Vite 的单元测试框架,专为现代前端项目设计. 它结合了 Vite 的高性能和 Jest 的易用性, 提供了开箱即用的 TypeScript.ESM ...

  8. 【自荐】Catime v1.0.4 一款贼好用的计时器

    Github: https://github.com/vladelaina/Catime 仅1.3MB!!!!! 特点 极简设计: 透明界面.点击穿透.可调大小和位置.多语言支持 丰富字体: 47种字 ...

  9. 记一次.NET内存居高不下排查解决与启示

    前情 我们有个海外的项目,一共70个服务,前前后后花了超过一年时间完成了云服务迁移和架构调整.主要是架构调整了,原来的docker swarm托管服务,新架构改为Kubernetes托管.几台云服务器 ...

  10. netcore后台服务慎用BackgroundService

    在 .NET Core 开发中,BackgroundService 是一个非常方便的后台任务运行方式,但它并不适用于所有场景. BackgroundService 一时爽,并发火葬场. Backgro ...