手写Spring:第5章-注入属性和依赖对象

文章目录

  • 一、目标:注入属性和依赖对象
  • 二、设计:注入属性和依赖对象
  • 三、实现:注入属性和依赖对象
    • 3.0 引入依赖
    • 3.1 工程结构
    • 3.2 注入属性和依赖对象类图
    • 3.3 定义属性值和属性集合
      • 3.3.1 定义属性值
      • 3.3.2 定义属性集合
    • 3.4 Bean定义补全
    • 3.5 Bean属性填充
  • 四、测试:注入属性和依赖对象
    • 4.1 用户Bean对象
      • 4.1.1 用户Dao对象
      • 4.1.2 用户Service对象
    • 4.2 单元测试
  • 五、总结:注入属性和依赖对象

一、目标:注入属性和依赖对象

💡 已经完成 实现一个容器、定义和注册Bean、实例化Bean、按照是否包含构造函数实现不同的实例化策略,那么在创建对象实例化,我们还缺少什么?

  • 其实还缺少 类中是否有属性的问题,如果类中包含属性那么在实例化的时候就需要把属性信息填充上,这样才是一个完整的对象创建。
  • 对于属性的填充不只是 int、Long、String,还包括没有实例化的对象属性,都需要在 Bean 创建时进行填充操作。

二、设计:注入属性和依赖对象

💡 技术设计:注入属性和依赖对象

  • 属性填充是在 Bean 使用 newInstance 或者 cglib 创建后,开始补全属性信息,那么就可以在类 AbstractAutowireCapableBeanFactorycreateBean 方法中添加补全属性方法。

在这里插入图片描述

  • 属性填充要在类实例化创建之后,也就是需要在 AbstractAutowireCapableBeanFactory#createBean 方法中添加 applyPropertyValues 操作。
  • 由于需要在创建 Bean 时填充属性操作,那么就需要在 bean 定义 BeanDefinition 类中,添加 PropertyValues 信息。
  • 另外是填充属性信息还包括 Bean 的对象类型,也就是需要再定义一个 BeanReference
    • 里面其实就是一个简单的 Bean 名称,再具体的实例化操作时进行递归创建和填充。
    • Spring 源码实现一样,Spring 源码中 BeanReference 是一个接口。

三、实现:注入属性和依赖对象

3.0 引入依赖

pom.xml

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.5.0</version>
</dependency>

3.1 工程结构

spring-step-04
|-src|-main| |-java|   |-com.lino.springframework|     |-factory|     | |-config|     | | |-BeanDefinition.java|     | | |-BeanReference.java|     | | |-SingletonBeanRegistry.java|     | |-support|     | | |-AbstractAutowireCapableBeanFactory.java|     | | |-AbstractBeabFactory.java|     | | |-BeanDefinitionRegistry.java|     | | |-CglibSubclassingInstantiationStrategy.java|     | | |-DefaultListableBeanFactory.java|     | | |-DefaultSingletonBeanRegistry.java|     | | |-InstantiationStrategy.java|     | | |-SimpleInstantiationStrategy.java|     | |-BeanFactory.java|     |-BeansException.java|     |-PropertyValue.java|     |-PropertyValues.java|-test|-java|-com.lino.springframework.test|-bean| |-UserDao.java| |-UserService.java|-ApiTest.java

3.2 注入属性和依赖对象类图

在这里插入图片描述

  • 新增加3个类,BeanReference(类引用)、PropertyValue(属性值)、PropertyValues(属性集合),分别用于类和其他类型属性填充操作。
  • 另外改动的类主要是 AbstractAutowireCapableBeanFactory,在 createBean 中补全属性填充部分。

3.3 定义属性值和属性集合

3.3.1 定义属性值

PropertyValue.java

package com.lino.springframework;/*** @description: Bean属性信息*/
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;}
}

3.3.2 定义属性集合

PropertyValues.java

package com.lino.springframework;import java.util.ArrayList;
import java.util.List;/*** @description: 属性值集合*/
public class PropertyValues {private final List<PropertyValue> propertyValueList = new ArrayList<>();public void addPropertyValue(PropertyValue pv) {this.propertyValueList.add(pv);}public PropertyValue[] getPropertyValues() {return this.propertyValueList.toArray(new PropertyValue[0]);}public PropertyValue getPropertyValue(String propertyName) {for (PropertyValue pv : this.propertyValueList) {if (pv.getName().equals(propertyName)) {return pv;}}return null;}
}
  • 这两个类的作用就是创建出一个用于传递类中属性信息的类,因为属性可能会有很多,所以还需要定义一个集合包装下。

3.4 Bean定义补全

BeanReference.java

package com.lino.springframework.factory.config;/*** @description: Bean 引用*/
public class BeanReference {private final String beanName;public BeanReference(String beanName) {this.beanName = beanName;}public String getBeanName() {return beanName;}
}
  • Bean 注册的过程中是需要传递 Bean 的信息。
  • 所以为了把属性一定交给 Bean 定义,所以这里填充了 PropertyValues 属性,同时把两个构造函数做了一些简单的优化,避免后面 for 循环时还得判断属性填充是否为空。

3.5 Bean属性填充

AbstractAutowireCapableBeanFactory.java

package com.lino.springframework.factory.support;import cn.hutool.core.bean.BeanUtil;
import com.lino.springframework.BeansException;
import com.lino.springframework.PropertyValue;
import com.lino.springframework.PropertyValues;
import com.lino.springframework.factory.config.BeanDefinition;
import com.lino.springframework.factory.config.BeanReference;
import java.lang.reflect.Constructor;/*** @description: 实现默认bean创建的抽象bean工厂超类*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();@Overrideprotected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {Object bean = null;try {bean = createBeanInstance(beanDefinition, beanName, args);// 给bean填充属性applyPropertyValues(beanName, bean, beanDefinition);} catch (Exception e) {throw new BeansException("Instantiation of bean failed", e);}registerSingletonBean(beanName, bean);return bean;}private void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {try {PropertyValues propertyValues = beanDefinition.getPropertyValues();for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {String name = propertyValue.getName();Object value = propertyValue.getValue();if (value instanceof BeanReference) {// A 依赖 B,获取 B 的实例化BeanReference beanReference = (BeanReference) value;value = getBean(beanReference.getBeanName());}// 属性填充BeanUtil.setFieldValue(bean, name, value);}} catch (Exception e) {throw new BeansException("Error setting property values: " + beanName);}}protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {Constructor constructorToUse = null;Class<?> beanClass = beanDefinition.getBeanClass();Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();for (Constructor ctor : declaredConstructors) {if (null != args && ctor.getParameterTypes().length == args.length) {constructorToUse = ctor;break;}}return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);}public InstantiationStrategy getInstantiationStrategy() {return instantiationStrategy;}public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {this.instantiationStrategy = instantiationStrategy;}
}
  • 这个类主要包括三个方法:createBeancreateBeanInstanceapplyPropertyValues,这里我们主要关注 createBean 的方法中调用的 applyPropertyValues 方法。
  • applyPropertyValues 中,通过获取 beanDefinition.getPropertyValues() 循环进行属性填充操作。
    • 如果遇到的是 BeanReference,那么就需要递归获取 Bean 实例,调用 getBean 方法。
  • 当把依赖的 Bean 对象创建完成后,会递归回现在属性填充中。

四、测试:注入属性和依赖对象

4.1 用户Bean对象

4.1.1 用户Dao对象

UserDao.java

package com.lino.springframework.test.bean;import java.util.HashMap;
import java.util.Map;/*** @description: 模拟用户DAO类*/
public class UserDao {private static Map<String, String> hashMap = new HashMap<>();static {hashMap.put("10001", "张三");hashMap.put("10002", "李四");hashMap.put("10003", "王五");}public String queryUserName(String uId) {return hashMap.get(uId);}
}

4.1.2 用户Service对象

UserServce.java

package com.lino.springframework.test.bean;/*** @description: 模拟用户 Bean 对象*/
public class UserService {private String uId;private UserDao userDao;/*** 查询用户信息*/public void queryUserInfo() {System.out.println("查询用户信息: " + userDao.queryUserName(uId));}public String getuId() {return uId;}public void setuId(String uId) {this.uId = uId;}public UserDao getUserDao() {return userDao;}public void setUserDao(UserDao userDao) {this.userDao = userDao;}
}
  • UserService 中注入 UserDao,这样就能体现出 Bean 属性的依赖。

4.2 单元测试

ApiTest.java

@Test
public void test_BeanFactory() {// 1.初始化 BeanFactoryDefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();// 2.UserDao注册beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));// 3.UserService 设置属性[uId、userDao]PropertyValues propertyValues = new PropertyValues();propertyValues.addPropertyValue(new PropertyValue("uId", "10001"));propertyValues.addPropertyValue(new PropertyValue("userDao", new BeanReference("userDao")));// 4.UserService 注入beanBeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues);beanFactory.registerBeanDefinition("userService", beanDefinition);// 5.获取beanUserService userService = (UserService) beanFactory.getBean("userService");userService.queryUserInfo();
}
  • 与直接获取 Bean 对象不同,这次我们还需要先把 userDao 注入到 Bean 容器中。
    • beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class))
  • 接下来就是属性填充的操作。
    • 一种是普通属性:new PropertyValue("uId", "10001")
    • 另外一种是对象属性:new PropertyValue("userDao", new BeanReference("userDao"))
  • 最后是正常获取 userService 对象,调用方法即可。

测试结果

查询用户信息: 张三

在这里插入图片描述

  • 从测试结果来看,属性填充已经起作用了,因为只有属性填充后,才能调用到 Dao 方法,如:userDao.queryUserName(uId)

五、总结:注入属性和依赖对象

  • 本章对 AbstructAutowireCapableBeanFactory 类中的创建对象功能又做了扩充,依赖于是否有构造函数的实例化策略完成后。
    • 开始补充 Bean 属性信息。当遇到 Bean 属性为 Bean 对象时,需要递归处理。
    • 最后在属性填充时需要用到反射操作,也可以使用一些工具类处理。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/100299.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《向量数据库指南》——AI原生向量数据库Milvus Cloud 2.3架构升级

架构升级 GPU 支持 早在 Milvus 1.x 版本,我们就曾经支持过 GPU,但在 2.x 版本中由于切换成了分布式架构,同时出于对于成本方面的考虑,暂时未加入 GPU 支持。在 Milvus 2.0 发布后的一年多时间里,Milvus 社区对 GPU 的呼声越来越高,再加上 NVIDIA 工程师的大力配合——为…

深度优先搜索(dfs)--矩阵部分-leetcode以及常见题

介绍 深度优先搜索&#xff08;Depth-First Search&#xff0c;DFS&#xff09;是一种常用的图搜索算法&#xff0c;它用于查找图或树数据结构中的路径或解决问题。下面是深度优先搜索的常见步骤以及一个示例问题&#xff1a; 深度优先搜索的常见步骤&#xff1a; 选择起始节…

Layui快速入门之第一节Layui的基本使用

目录 一&#xff1a;Layui的基本概念 二&#xff1a;Layui使用的基本步骤 1.在官网下载layui的基本文件&#xff0c;引入css和js文件 ①&#xff1a;普通方式引入 ②&#xff1a;第三方 CDN 方式引入 2.在script标签体中编写代码 3.测试 一&#xff1a;Layui的基本概念 …

flink学习之广播流与合流操作demo

广播流是什么&#xff1f; 将一条数据广播到所有的节点。使用 dataStream.broadCast() 广播流使用场景&#xff1f; 一般用于动态加载配置项。比如lol&#xff0c;每天不断有人再投诉举报&#xff0c;客服根本忙不过来&#xff0c;腾讯内部做了一个判断&#xff0c;只有vip3…

Hadoop的第二个核心组件:MapReduce框架第四节

Hadoop的第二个核心组件&#xff1a;MapReduce框架 十、MapReduce的特殊应用场景1、使用MapReduce进行join操作2、使用MapReduce的计数器3、MapReduce做数据清洗 十一、MapReduce的工作流程&#xff1a;详细的工作流程第一步&#xff1a;提交MR作业资源第二步&#xff1a;运行M…

升哲科技城市级“算力+数字底座”服务亮相2023服贸会

9月2日至6日&#xff0c;以“开放引领发展&#xff0c;合作共赢未来”为主题的2023年中国国际服务贸易交易会在北京隆重举办。作为城市级数据服务商&#xff0c;升哲科技&#xff08;SENSORO&#xff09;连续第四年参加服贸会&#xff0c;携城市级“算力数字底座”服务及在城市…

【Apollo】自动驾驶技术的介绍

阿波罗是百度发布的名为“Apollo&#xff08;阿波罗&#xff09;”的向汽车行业及自动驾驶领域的合作伙伴提供的软件平台。 帮助汽车行业及自动驾驶领域的合作伙伴结合车辆和硬件系统&#xff0c;快速搭建一套属于自己的自动驾驶系统。 百度开放此项计划旨在建立一个以合作为中…

halcon双目标定双相机标定

halcon双目标定 *取消更新 dev_update_off () *获取窗体句柄 dev_get_window (WindowHandle) *设置窗体字体样式 set_display_font (WindowHandle, 16, mono, true, false) *设置线条粗细 dev_set_line_width (3) *创建空对象 gen_empty_obj (ImageL) *读取指定文件内子集 li…

实现CenterNet图像分割算法模型的转换和量化(SDK0301-转ONNX编译)

一、实现CenterNet图像分割算法模型的转换和量化&#xff08;SDK0301-转ONNX编译&#xff09; 1、模型转换 &#xff08;1&#xff09;下载CenterNet算法移植代码&#xff1a; $ git clone https://github.com/sophon-ai-algo/examples.git # CenterNet示例项目代码位置 /ex…

Yolov5的tensorRT加速(python)

地址&#xff1a;https://github.com/wang-xinyu/tensorrtx/tree/master/yolov5 下载yolov5代码 方法一&#xff1a;使用torch2trt 安装torch2trt与tensorRT 参考博客&#xff1a;https://blog.csdn.net/dou3516/article/details/124538557 先从github拉取torch2trt源码 ht…

Power BI的发布到web按钮怎么没有?有人知道怎么办吗??????

Power BI的发布到web按钮怎么没有&#xff1f;有人知道怎么办吗&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#xff1f; .

docker启动paddlespeech服务,并使用接口调用

一、检查docker容器是否启动 1.输入命令 systemctl status docker 启动 systemctl start docker 守护进程重启 sudo systemctl daemon-reload 重启docker服务 systemctl restart docker 重启docker服务 sudo service docker restart 关闭docker service docker…