主要内容

添加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. PS2021、色环插件Coolorus下载+色环无法拖动问题和没有压感问题的解决

    PS2021+色环插件Coolorus+色环无法拖动问题解决+没有压感问题解决 1.资源下载链接 PS各版本下载: 感谢微博@vposy大佬无私奉献提供的各版本ADOBE软件直装破解: 百度网盘: p ...

  2. Spring Cloud的5大核心组件详解

    Spring Cloud Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而 ...

  3. Mysql调优之使用mysql慢查询日志优化sql语句及表索引

    Mysql调优之使用mysql慢查询日志优化sql语句及表索引 一,用慢查询日志找出耗时语句,并优化 # 查看mysql系统慢查询变量配置(能看到慢查询日志是否开启,日志路径等) SHOW VARIA ...

  4. Linux 虚拟机中不重启的情况下加新硬盘及扩展根分区容量

    我这个系统是Redhat7.7的系统.磁盘占用比较高,需要扩充空用空间,同时又不能关停服务器,或者服务.所以就需要在虚拟机中不重启的情况下加新硬盘及扩展根分区容量. 首先,看一下我这个虚拟机分区占用情 ...

  5. ABC243

    ABC224 D 题目大意 有一个九个点的无向图棋盘,上面有八个棋子,一次操作能将一个棋子沿边移到空点上,问将每个棋子移到与它编号相同的点最少几步. 解题思路 考虑使用 BFS. 用 string 存 ...

  6. Zino pg walkthrough Intermediate

    nmap 扫描 发现smba共享文件 ┌──(root㉿kali)-[~] └─# nmap -p- -A 192.168.167.64 Starting Nmap 7.94SVN ( https:/ ...

  7. Pinely Round 4 (Div. 1 + Div. 2)

    题目链接:Pinely Round 4 (Div. 1 + Div. 2) 总结:被B卡了一年. A. Maximize the Last Element tag:模拟 Description:给定一 ...

  8. Jenkins插件:Publish over SSH

    Jenkins插件:Publish over SSH Jenkins作为一个开源的持续集成和交付工具,通过插件扩展可以实现各种功能.其中,Publish over SSH插件是Jenkins的一个常用 ...

  9. Q:批处理备份临时文件

    批处理:将本月临时文件移动到历史目录中 chcp 936 @echo off set month=%date:~2,2%%date:~5,2% set year=%date:~0,4% echo %y ...

  10. 记一次腾讯云轻量级服务器安装mysql配置完成后,外网无法访问问题

    一.配置信息正常 1.防火墙配置通过 2.mysql端口正常启动netstat -antlp | grep 3306 3.配置都正常,但是telnet访问不通超时Operation timed out ...