主要内容

添加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. 记一次 .NET某汗液测试机系统 崩溃分析

    一:背景 1. 讲故事 上个月在社区写的文章比较少,一直关注的朋友应该知道那段时间被狗咬了以及一些琐事处理,所以手头上也攒了不少需要分享的案例,这段时间比较空闲,逐个给大 家做个分享吧,刚好年后为新版 ...

  2. JVM实战—6.频繁YGC和频繁FGC的后果

    大纲 1.JVM GC导致系统突然卡死无法访问 2.什么是Young GC什么是Full GC 3.Young GC.Old GC和Full GC的发生情况 4.频繁YGC的案例(G1解决大内存YGC ...

  3. 2022 年数据科学研究综述:重点介绍 ML、DL、NLP 等

    2022 年数据科学研究综述:重点介绍 ML.DL.NLP 等 当我们在 2022 年底临近时,我对许多著名研究小组完成的所有惊人工作感到振奋,他们将 AI.机器学习.深度学习和 NLP 的状态扩展到 ...

  4. Java内存模型深度剖析

    为什么要有内存模型  在介绍Java内存模型之前,先来看一下到底什么是计算机内存模型,然后再来看Java内存模型在计算机内存模型的基础上做了哪些事情.要说计算机的内存模型,就要说一下一段古老的历史,看 ...

  5. 第六章 dubbo源码解析目录

    13.1 dubbo服务降级源码解析 从 9.1 客户端发起请求源码 的客户端请求总体流程图中,截取部分如下: //代理发出请求 proxy0.sayHello(String paramString) ...

  6. Robot Framework 自动化测试部署常见问题及处理方法(二)

    书接上文 4.使用Open Browser关键字打开浏览器报错"WebDriverException: Message: 'geckodriver' executable needs to ...

  7. C++:Eigen库

    了解C++的Eigen库,主要内容来自:https://blog.csdn.net/hongge_smile/article/details/107296658 ,并加入自己的笔记. 介绍 Eigen ...

  8. Java中用Deque接口代替Stack接口完成栈功能

    之前在有需要用到栈功能的时候,都是通过使用Stack接口完成的,也就是: 1 Stack<T> stack = new Stack<>() 但今天突然发现,Java Doc里建 ...

  9. 《SpringBoot》史上最全SpringBoot相关注解介绍

    @SpringBootApplication @SpringBootApplication看作是 @Configuration.@EnableAutoConfiguration.@ComponentS ...

  10. SQL server 更改计算机名后造成未找到或无法访问服务器解决方法

    默认的计算机名较长且不易辨识,我在更改完计算机名之后却发现每次登陆SQL server都需要更改计算机名并重启计算机,否则便会出现以下错误提示: 此时我们需要再次更改计算机名(最终你想给计算机起的名字 ...