主要内容

  • 添加PropertyValue类表示Bean的属性。

  • 为Bean定义对象BeanDefinition添加PropertyValues列表用来存储Bean的各种属性。

  • Bean实例化时根据PropertyValues填充属性。

代码分支

populate-bean-with-property-values

核心代码

PropertyValue

表示类的属性

public class PropertyValue {
//属性名
private final String name;
//属性值
private final Object value;
public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
}

PropertyValues

内部维护一个PropertyValue列表,维护bean的各种属性。

ublic class PropertyValues {
private final List<PropertyValue> propertyValueList = new ArrayList<PropertyValue>();
public void addPropertyValue(PropertyValue propertyValue) {
this.propertyValueList.add(propertyValue);
} public PropertyValue[] getPropertyValues() {
return propertyValueList.toArray(new PropertyValue[0]);
} public PropertyValue getPropertyValue(String propertyName){
for(int i = 0; i < propertyValueList.size(); i++) {
PropertyValue propertyValue = propertyValueList.get(i);
if(propertyValue.getName().equals(propertyName)){
return propertyValue;
}
}
return null;
}
}

BeanDefinition

public class BeanDefinition {
private Class clazz;
private PropertyValues propertyValues; public BeanDefinition() {
}
public BeanDefinition(Class clazz) {
this.clazz = clazz;
}
public BeanDefinition(Class clazz, PropertyValues propertyValues) {]
this.clazz = clazz;
this.propertyValues = propertyValues;
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
public PropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
}

AbstractAutowireCapableBeanFactory

doCreateBean方法

Bean实例化时注入属性

        protected Object doCreateBean(String beanName, BeanDefinition beanDefinition) {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition);
//为bean填充属性
applyPropertyValues(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new BeansException("Instantiation of bean failed", e);
}
addSingleton(beanName, bean);
return bean;
}

applyPropertyValues方法

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

测试

    @Test
public void test(){
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
PropertyValue propertyValue1 = new PropertyValue("name", "zhangSan");
PropertyValue propertyValue2 = new PropertyValue("age", 15);
PropertyValues propertyValues = new PropertyValues();
propertyValues.addPropertyValue(propertyValue1);
propertyValues.addPropertyValue(propertyValue2);
BeanDefinition beanDefinition = new BeanDefinition(Person.class, propertyValues);
factory.registerBeanDefinition("person", beanDefinition);
Person person = (Person)factory.getBean("person");
System.out.println(person);
}

测试结果

Person{name='zhangSan', age='15'}

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

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

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

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

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

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

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

  4. 曹工说Spring Boot源码(27)-- Spring的component-scan,光是include-filter属性的各种配置方式,就够玩半天了.md

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  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. 监听sessionStorage中值的变化

    应用场景:将登录接口所返回的用户信息存入sessionStorage,在其他地方需要使用到用户信息,但不知道什么时候接口请求完,此时可以监听sessionStorage中值的变化. 应用:Event自 ...

  2. LCR 164. 破解闯关密码

    破解闯关密码 闯关游戏需要破解一组密码,闯关组给出的有关密码的线索是: 一个拥有密码所有元素的非负整数数组 password 密码是 password 中所有元素拼接后得到的最小的一个数 请编写一个程 ...

  3. refs转发

    ref 转发不但可以转发指向具体的dom组件,也可以指向class组件的实例 import React from 'react' import ReactDOM from 'react-dom'; / ...

  4. 初识MinIO

    writer:zgx last modify: 2020年09月26日 目录 前言 MinIO简介 MinIO使用 MinIO安装 MinIO纠删码 纠删码ensure code是什么 refers ...

  5. Svelte 最新中文文档翻译(1)—— 概述与入门指南

    前言 Svelte,一个非常"有趣".用起来"很爽"的前端框架.从 Svelte 诞生之初,就备受开发者的喜爱,根据统计,从 2019 年到 2024 年,连续 ...

  6. Akka中使用Logback日志框架

    Akka提供的默认日志系统只输出到控制台,这种日志系统不可以用到产品环境,当然你可以整合SLF4J这样的日志系统,下面介绍如何在Akka中使用Logback记录日志. 1. 创建Maven工程引入相关 ...

  7. SNMP简介

    复习几组概念带内管理和带外管理区别:传送的物理通道不同.1.带内管理是管理控制信息与数据信息使用统一物理通道进行传送.当网络出现故障中断时数据传输和管理都无法正常进行.2.带外管理在于通过不同的物理通 ...

  8. Luogu P11628 WC2025 猫粮 题解 [ 绿 ] [ 贪心 ] [ adhoc ] [ 鸽巢原理 ]

    猫粮:WC 诈骗题.我竟然能切 WC 的 T3 也是逆天了. 话说切了猫粮能变成猫娘吗 qwq. 思路 首先题目里有下面几点关键的性质: 所有猫粮质量总和等于所有猫要吃的质量总和. 优质的有 \(n\ ...

  9. Luogu P10869 LCMs 题解 [ 黄 ] [ lcm ] [ 最短路 ]

    LCMs:很好的数论和构造题. 显然我们不可以直接建图跑最短路. 于是考虑分讨. 倍数关系 答案显然为 \(\max(a,b)\). 相等关系 答案显然为 \(0\). \(\gcd(a,b)> ...

  10. 天线驻波比&回波损耗

    天线驻波比(VSWR)‌,全称为电压驻波比,是衡量天线系统匹配程度的重要参数.它定义为驻波波腹处的电压幅值与波谷处的电压幅值之比.理想情况下,当馈线和天线的阻抗完全匹配时,驻波比为1,表示高频能量全部 ...