手写Spring4(Spring属性填充)

news2024/9/24 3:24:28

文章目录

  • 目标
  • 设计思路
  • 项目结构
  • 一、实现
    • 1、定义属性
    • 2、定义属性集合包装类
    • 3、类引用(bean类型)
    • 4、Bean定义补全
    • 5、Bean 属性填充
  • 二、测试
    • 1、准备
    • 2、测试案例
    • 3、测试结果


目标

这一章节目的是接着前面的实例化策略实现后,进行属性填充,才算是真正的实例化结束

今天这里暂时不考虑循环依赖,后续会加上


设计思路

该图来自小博哥
主要做了几件事情

1、属性填充是在创建实例化后,在AbstractAutowireCapableBeanFactory 的createBean方法中添加applyPropertyValues 操作。

2、由于我们需要在创建Bean时候填充属性操作,那么就需要在 bean 定义 BeanDefinition 类中,添加 PropertyValues 信息。

3、填充属性还包括的bean的对象类型,所以需要添加一个BeanReference(区分于普通属性类型,如果是bean对象则递归进行创建和填充)


项目结构

在这里插入图片描述

核心关系图

在这里插入图片描述

新增加3个类,BeanReference(类引用)、PropertyValue(属性值)、PropertyValues(属性集合),分别用于类和其他类型属性填充操作。

另外改动的类主要是 AbstractAutowireCapableBeanFactory,在 createBean 中补全属性填充部分


一、实现

1、定义属性

/**
 * 属性
 */
public class PropertyValue {

    private final String name;

    private final Object value;


    public PropertyValue(String name, @Nullable Object value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public Object getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "PropertyValue{" +
                "name='" + name + '\'' +
                ", value=" + value +
                '}';
    }
}

属性的包装类


2、定义属性集合包装类

/**
 * 属性集合
 */
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 propertyValue : propertyValueList) {
            if(propertyValue.getName().equals(propertyName)){
                return propertyValue;
            }
        }
        return null;
    }

}

维护每个类下的属性集合


3、类引用(bean类型)

/**
 * 类引用
 */
public class BeanReference {

    private String beanName;

    public String getBeanName() {
        return beanName;
    }

    public BeanReference(String beanName) {
        this.beanName = beanName;
    }
}

在属性填充时,循环处理时,用于区分是一个bean类型还是一个普通类型,是bean类型则需要递归去创建和获取bean,普通属性则直接赋值


4、Bean定义补全

/**
 * @desc Bean定义
 * @Author: ljc
 * @Date: 2022/11/28 10:35
 */
public class BeanDefinition {

    private Class beanClass;

    private PropertyValues propertyValues;

    public BeanDefinition(Class beanClass) {
        this.beanClass = beanClass;
        this.propertyValues = new PropertyValues();
    }

    public BeanDefinition(Class beanClass,PropertyValues propertyValues) {
        this.beanClass = beanClass;
        this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
    }


    public Class getBeanClass() {
        return beanClass;
    }

    public void setBeanClass(Class beanClass) {
        this.beanClass = beanClass;
    }

    public PropertyValues getPropertyValues() {
        return propertyValues;
    }

    public void setPropertyValues(PropertyValues propertyValues) {
        this.propertyValues = propertyValues;
    }
}

新增了1个构造函数,方便初始化bean定义时,直接填充属性


5、Bean 属性填充

/**
 * @desc 实例化Bean类
 * @Author: ljc
 * @Date: 2022/12/7 13:06
 */
public abstract class  AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{

    private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();

    /**
     * 创建bean
     * @param beanName
     * @param beanDefinition
     * @param args
     * @return
     * @throws BeansException
     */
    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
        Object bean = null;
        try {
            bean = createBeanInstance(beanName,beanDefinition,args);
            // 属性填充
            applyPropertyvalues(beanName,bean,beanDefinition);
        } catch (Exception e) {
            throw new BeansException("Instantiation of bean failed", e);
        }
        addSingleton(beanName,bean);
        return bean;
    }


    /**
     * 创建实例
     * @param beanName
     * @param beanDefinition
     * @param args
     * @return
     */
    protected Object createBeanInstance(String beanName, BeanDefinition  beanDefinition, Object[] args) {
        Constructor constructorToUse  = null;
        Class beanClass = beanDefinition.getBeanClass();
        Constructor[] declaredConstructors = beanClass.getDeclaredConstructors();
        for (Constructor ctor : declaredConstructors) {
            if (args != null && ctor.getParameterTypes().length == args.length) {
                constructorToUse  = ctor;
                break;
            }
        }
        return getInstantiationStrategy().instantiate(beanDefinition,beanName,constructorToUse,args);
    }


    /**
     * 属性填充
     * @param beanName
     * @param bean
     * @param beanDefinition
     */
    protected 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) {
                    // 获取 依赖的对象实例化
                    BeanReference beanReference = (BeanReference) value;
                    value = getBean(beanReference.getBeanName());
                }
                BeanUtil.setFieldValue(bean, name, value);
            }
        } catch (BeansException e) {
            throw new BeansException("Error setting property values:" + beanName);
        }
    }

    /**
     * 获取实例化策略
     * @return
     */
    public InstantiationStrategy getInstantiationStrategy() {
        return instantiationStrategy;
    }

    // 定义实例化策略
    public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
        this.instantiationStrategy = instantiationStrategy;
    }
}

这个类的内容稍微有点长,主要包括三个方法:createBean、createBeanInstance、applyPropertyValues,这里我们主要关注 createBean 的方法中调用的 applyPropertyValues 方法。

1、在 applyPropertyValues 中,通过获取 beanDefinition.getPropertyValues() 循环进行属性填充操作,如果遇到的是 BeanReference,那么就需要递归获取 Bean 实例,调用 getBean 方法。

2、当把依赖的 Bean 对象创建完成后,会递归回现在属性填充中。这里需要注意我们并没有去处理循环依赖的问题,这部分内容较大,后续补充

3、BeanUtil.setFieldValue(bean, name, value) 是 hutool-all 工具类中的方法,你也可以自己实现


二、测试

1、准备

public class UserDao {

    private static Map<String, String> hashMap = new HashMap<>();

    static {
        hashMap.put("10001", "ljc");
        hashMap.put("10002", "yaya");
        hashMap.put("10003", "zz");
    }

    public String queryUserName(String uId) {
        return hashMap.get(uId);
    }
}
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;
    }
}

2、测试案例

public class ApiTest {

    @Test
    public void test_BeanFactory() {
        // 1、初始化 BeanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 2、userDao 注册bean定义
        beanFactory.registerBeanDefinition("userDao",new BeanDefinition(UserDao.class));
        // 3、userService设置属性
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("uId","10002"));
        propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao")));
        // 4 、userService注入bean
        BeanDefinition beanDefinition = new BeanDefinition(UserService.class,propertyValues);
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        // 5、获取 bean
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.queryUserInfo();
    }


}

1、与直接获取 Bean 对象不同,这次我们还需要先把 userDao 注入到 Bean 容器中。

beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));

2、接下来就是属性填充的操作了,一种是普通属性 new PropertyValue(“uId”, “10001”),另外一种是对象属性 new PropertyValue(“userDao”,new BeanReference(“userDao”))

3、接下来的操作就简单了,只不过是正常获取 userService 对象,调用方法即可。


3、测试结果

查询用户信息:yaya

Process finished with exit code 0

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MybatisPlus概述及使用

文章目录MybatisPlus1、介绍1.1、概述1.2、特性1.3、 支持数据库1.4、框架结构2、起步依赖与核心接口2.1 起步依赖2.2 BaseMapper3、快速入门3.1、导入依赖3.2、SpringBoot配置文件3.3、实体类3.4、Mapper接口3.5、引导类增加MapperScan注解3.6 测试4、注解4.1 TableName4.2 Ta…

《从总账到总监》读书笔记

书本封面 内容简介 这是一本用小说体例写作的财务专业书籍。工作中如何将财物数据业务化&#xff0c;找到绩效管理的关键点&#xff1f;在人工智能都会写诗的时代&#xff0c;财务这门学科是否会毫无价值&#xff1f;以及在未来&#xff0c;财务人员应该具备哪些专项能力才能不…

Java#34(多线程)

目录 线程(thread): 是一个程序内部的一条执行路经(之前写的代码中,main方法的执行就是一条单独的执行路径) 单线程: 一个程序只有一条执行路线 多线程: 指从软硬件上实现多条执行流程的技术 一.创建多线程 1.继承Thread类 2.实现Runnable接口 3.利用Callable, FutureT…

8 标志寄存器

标志寄存器 CPU 内部的寄存器中&#xff0c;有一种特殊的寄存器&#xff08;对于不同的处理机&#xff0c;个数和结构都可能不同&#xff09;具有以下3 种作用。 用来存储相关指令的某些执行结果&#xff1b;用来为CPU 执行相关指令提供行为依据&#xff1a;用来控制CPU 的相…

Pyinstaller打包exe程序

Pyinstaller和Nuitka是两大热门的python打包路径&#xff0c;学习Nuitka可以点击Nuitka入门学习。本文主要介绍Pyinstaller打包过程。 1.创建python虚拟环境 在conda中创建一个虚拟环境&#xff0c;用于程序打包&#xff0c;可以使打包程序占用空间最小 使用一下指令&#x…

一个超方便将现有博客生成vuepress2文档站的插件

闲来无事&#xff0c;研究了一下vuepress2和它的插件机制&#xff0c;写了一个可以一键通过已有博客生成vuepress2的文档站的vuepress2插件vuepress-plugin-blog-sync 效果 在vuepress2中简单引入即可达到将政采云掘金博客一键生成vuepress2页面&#xff0c;效果✨ 详见Demo …

38_SPI通信原理

SPI接口简介 SPI是英语Serial Peripheral interface的缩写,顾名思义就是串行外围设备接口。是Motorola首先在其MC68HCXX系列处理器上定义的。 SPI,是一种高递的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便,…

蔚来智驾功能大更新:与其叫NOP+,不如叫NAD-

HiEV消息&#xff08;文/张祥威&#xff09;赶在2022年最后一个月&#xff0c;蔚来的自动驾驶大招露出了冰山一角。 2020年10月&#xff0c;蔚来国内首家推送高速场景下的领航辅助NOP&#xff08;Navigate on Pilot&#xff09;&#xff0c;当时只有特斯拉实现了类似功能&#…

English Learning - L1 站在高处建立灵魂 2022.12.5 周一

English Learning - L1 站在高处建立灵魂 2022.12.5 周一1.1 到底什么是语法1.2 为什么要学习语法口语分广义和狭义讲母语的人为啥不学语法&#xff1f;作为一名二语习得者口语中可不可以没有有语法&#xff1f;1.3 英语&#xff08;听说读写&#xff09;的核心金字塔理论关于词…

[附源码]计算机毕业设计基于web的羽毛球管理系统Springboot程序

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

数据结构作业——第十四周——AOE网+查找

1. 单选题 简单 5分 若一个有向图中的顶点不能排成一个拓扑序列&#xff0c;则可断定该有向图______。 A.是个有根有向图 B.是个强连通图 C.含有多个入度为0的顶点 D.含有顶点数目大于1的强连通分量 回答正确 解析 2 . 单选题 简单 5分 以下关于图拓扑排序的叙述中正确的…

自动驾驶之去光晕调研

中文版综述github 一、光晕类型 常见去光晕算法的光晕 去光晕算法的光晕之二(汇总) 样式包括有: halos(色圈), streaks(条纹), bright lines(亮线), saturated blobs(深污点), color bleeding(色渗), haze(烟雾), and many others。 二、光晕成因 一个理想的相机&#x…

JSP SSH校园兼职信息发布平台myeclipse开发mysql数据库MVC模式java编程计算机网页设计

一、源码特点 JSP SSH校园兼职信息发布平台是一套完善的web设计系统&#xff08;系统采用ssh框架进行设计开发&#xff09;&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采 用B/S模式开发。开发环境为TOMCA…

Docker Images Explore

Docker Images Explore scratch an explicitly empty image, especially for building images “FROM scratch” You can use Docker’s reserved, minimal image, scratch, as a starting point for building containers. Using the scratch “image” signals to the build …

自定义键盘快捷键调节电脑音量

外接的键盘没有Fn。。。也没有调音量的键&#x1f605;于是想自己弄一个 方法一&#xff1a;修改注册表键盘映射 新建记事本文件打开&#xff0c;粘贴如下内容&#xff1a; Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Key…

Idea 调试自定义 AbstractProcessor 程序

我们常使用的 Lombok 可以自动生成 gettr 和 setter 方法,使用起来非常方便;有时候我们也要去实现自己的注解处理器,实现一些在编译阶段需要执行的逻辑,例如我之前写的 spring cloud 自动生成 openfeign 的Fallback 降级方法;自动生成 openfeign 的Fallback 但是自定义注…

JavaEE进阶:SpringBoot概念、创建和运⾏

文章目录一、Spring Boot 优点二、Spring Boot 项⽬创建1、使用 Idea 创建① 准备工作② 创建项目③ 注意事项2、网页版创建&#xff08;了解&#xff09;三、项目目录介绍和运行1、运行项目2、输出 Hello world四、注意事项&#xff1a;包路径错误1、正确路径2、小结&#xff…

第十四届蓝桥杯集训——JavaC组第九篇——三元运算符

第十四届蓝桥杯集训——JavaC组第九篇——三元运算符 一元运算符(一元运算符有1个操作数) &#xff0c;- -都是运算符&#xff0c;- -可分为前&#xff0c;后&#xff0b;和前-&#xff0c;后减如果在后面&#xff0c;如&#xff1a;num 10&#xff1b;先参与运算&#xff0c;然…

基于混合NSGA II-MOPSO算法的热电联合经济排放调度(Matlab代码实现)【混合多目标遗传算法-多目标粒子群算法】

&#x1f4a5;&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️❤️&#x1f4a5;&#x1f4a5;&#x1f4a5; &#x1f680;支持&#xff1a;&#x1f381;&#x1f381;&#x1f381;如果觉得博主的文章还不错或者您用得到的话&…

游戏开发43课 移动游戏性能优化1

1. 前言 很多年前就想将这些年工作中积累的优化经验撰写成文章&#xff0c;但懒癌缠身&#xff0c;迟迟未动手&#xff0c;近期总算潜下心写成文章。 涉及到具体优化技巧时&#xff0c;博主会尽量阐述原理和依据&#xff0c;让读者知其然也知其所以然。要完全读懂这篇文章&am…