[源码系列:手写spring] IOC第五节:Bean注入Bean
主要内容
添加BeanReference类,包装一个bean对另一个bean的引用。如beanA引用beanB,那么在实例化beanA时,如果propertyValue.value是BeanReference类型,引用beanB,那么先实例化beanB。在这里为了便于大家理解暂时不引入三级缓存解决循环依赖,三级缓存会在后面高级篇单独讲解。
代码分支
核心代码
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的更多相关文章
- Spring源码分析 手写简单IOC容器
Spring的两大特性就是IOC和AOP. IOC Container,控制反转容器,通过读取配置文件或注解,将对象封装成Bean存入IOC容器待用,程序需要时再从容器中取,实现控制权由程序员向程序的 ...
- Spring IOC(五)依赖注入
Spring IOC(五)依赖注入 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.autowire 五种注入方式测试 ...
- 《四 spring源码》手写springioc框架
手写SpringIOCXML版本 /** * 手写Spring专题 XML方式注入bean * * * */ public class ClassPathXmlApplicationContext { ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- 从零开始手写 spring ioc 框架,深入学习 spring 源码
IoC Ioc 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理. 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过 ...
- 框架源码系列六:Spring源码学习之Spring IOC源码学习
Spring 源码学习过程: 一.搞明白IOC能做什么,是怎么做的 1. 搞明白IOC能做什么? IOC是用为用户创建.管理实例对象的.用户需要实例对象时只需要向IOC容器获取就行了,不用自己去创建 ...
- 《四 spring源码》手写springmvc
手写SpringMVC思路 1.web.xml加载 为了读取web.xml中的配置,我们用到ServletConfig这个类,它代表当前Servlet在web.xml中的配置信息.通过web.xml ...
- Spring源码 20 手写模拟源码
参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...
- 利用递归,反射,注解等,手写Spring Ioc和Di 底层(分分钟喷倒面试官)了解一下
再我们现在项目中Spring框架是目前各大公司必不可少的技术,而大家都知道去怎么使用Spring ,但是有很多人都不知道SpringIoc底层是如何工作的,而一个开发人员知道他的源码,底层工作原理,对 ...
- Spring源码剖析3:Spring IOC容器的加载过程
本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...
随机推荐
- 有道云笔记默认的笔记格式转markdown
目录 0. 前言 1. 有道云笔记自带的笔记格式转markdown的方案 1.1 pdf => md 1.2 pdf => word => md 2. Markdown技巧 2.1 ...
- 使用Docker编译PaddlePaddle
在ubuntu中使用Docker编译PaddlePaddle 要在ubuntu中使用docker编译paddle框架,首先分为以下几个步骤: 安装docker环境 拉取paddle的docker镜像 ...
- VS2022推送代码 到github错误: CertGetCertificateChain trust error CERT_TRUST_IS_PARTIAL_CHAIN的解决办法
VS2022推送代码 到github错误: CertGetCertificateChain trust error CERT_TRUST_IS_PARTIAL_CHAIN 解决办法: 进入到 选项&g ...
- 通过WebView2获取HTTP-only cookie
通过WebView2获取HTTP-only cookie可以使用`WebView2.CookieManager`类的方法.以下是一个示例代码,演示如何获取HTTP-only cookie: using ...
- 免费-高清免费视频录像软件OBS
OBS studio 是免费开源的. https://obsproject.com/download 中文绿色版: http://www.xitongzhijia.net/soft/151705.ht ...
- 【Netty】(3)—源码NioEventLoopGroup
netty(3)-源码NioEventLoopGroup 一.概念 NioEventLoopGroup对象可以理解为一个线程池,内部维护了一组线程,每个线程负责处理多个Channel上的事件,而一个C ...
- 再谈Redux
2025年再聊前端状态管理似乎是一件不必要的事,毕竟相关文章已堆积得如山如海.但在这些文章或视频内容中,我并没有找到自己喜欢的方案,准确的说是使用方式.所以这篇文章不做技术分析,主要聊聊个人对状态管理 ...
- thewall靶机
includes.php 内有文件读取漏洞 一开始是想着直接用为协议写入一句话木马但是后来发现不行 因为他的文件读取方式长这样 点击查看代码 <?php include ('/var/www/h ...
- Idea报错 【cannot access com.xxx】的【解决办法】
正常操作代码,一个类突然标红,提示 cannot access com.xxx . 执行下面操作,执行完后项目变为正常 但是如果是你代码存在异常,可不是清缓存就能解决了,就要靠自己了哈哈 第一步Fil ...
- 使用C# 调用deepseek api接口,来实现正常访问
先上图,结果如图 先去官方网站充值api费用,默认 对应的C#代码封装 1 public class DeepSeekHelper 2 { 3 private static readonly Http ...