手写简易版Spring框架(三):属性填充

目标

首先我们回顾下这几章节都完成了什么,包括:实现一个容器、定义和注册Bean、实例化Bean,按照是否包含构造函数实现不同的实例化策略,那么在创建对象实例化这我们还缺少什么?其实还缺少一个关于类中是否有属性的问题,如果有类中包含属性那么在实例化的时候就需要把属性信息填充上,这样才是一个完整的对象创建。

设计

鉴于属性填充是在 Bean 使用 newInstance 或者 Cglib 创建后,开始补全属性信息,那么就可以在类 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加补全属性方法。
在这里插入图片描述

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

实现

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

定义属性

package com.qingyun.springframework.beans.factory;

/**
 * @description: bean实例中的一个属性
 * @author: 張青云
 * @create: 2021-08-18 23:47
 **/
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;
    }
}

package com.qingyun.springframework.beans.factory;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 一个bean对象中全部属性的集合
 * @author: 張青云
 * @create: 2021-08-18 23:48
 **/
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]);  // list的toArray方法需要传入一个数组来指定类型
    }

    public PropertyValue getPropertyValue(String propertyName) {
    
    
        for (PropertyValue pv : this.propertyValueList) {
    
    
            if (pv.getName().equals(propertyName)) {
    
    
                return pv;
            }
        }
        return null;
    }
}

这两个类的作用就是创建出一个用于传递类中属性信息的类,因为属性可能会有很多,所以还需要定义一个集合包装下。

Bean定义补全

package com.qingyun.springframework.beans.factory.config;

import com.qingyun.springframework.beans.factory.PropertyValues;

/**
 * @description: Bean的定义信息,对应于spring源码中解析xml文件后获取到的Bean定义信息
 * @author: 張青云
 * @create: 2021-08-18 16:52
 **/
public class BeanDefinition {
    
    
    //  bean的类型
    private Class beanClass;

    //  bean的属性信息
    private PropertyValues propertyValues;

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

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

    public Class getBeanClass() {
    
    
        return beanClass;
    }

    public PropertyValues getPropertyValues() {
    
    
        return propertyValues;
    }
}

在 Bean 注册的过程中是需要传递 Bean 的信息,在几个前面章节的测试中都有所体现 new BeanDefinition(UserService.class, propertyValues);所以为了把属性一定交给 Bean 定义,所以这里填充了 PropertyValues 属性,同时把两个构造函数做了一些简单的优化,避免后面 for 循环时还得判断属性填充是否为空。

Bean 属性填充

package com.qingyun.springframework.beans.factory.support;

import cn.hutool.core.bean.BeanUtil;
import com.qingyun.springframework.beans.BeansException;
import com.qingyun.springframework.beans.factory.PropertyValue;
import com.qingyun.springframework.beans.factory.PropertyValues;
import com.qingyun.springframework.beans.factory.config.BeanDefinition;
import com.qingyun.springframework.beans.factory.config.BeanReference;

import java.lang.reflect.Constructor;

/**
 * @description: 实现了根据BeanDefinition去创建bean的能力
 * @author: 張青云
 * @create: 2021-08-18 18:29
 **/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{
    
    
    //  指定实例化方式的策略,默认使用JDK反射的方式
    private InstantiationStrategy instantiation = new SimpleInstantiationStrategy();

    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
    
    
        Object bean = null;
        try {
    
    
            //  实例化bean
            bean = createBeanInstance(beanDefinition, beanName, args);
            //  对bean对象进行属性填充
            if (beanDefinition.getPropertyValues() != null &&
                    beanDefinition.getPropertyValues().getPropertyValues().length != 0) {
    
    
                applyPropertyValues(beanName, bean, beanDefinition);
            }
        } catch (Exception e) {
    
    
            throw new BeansException("Instantiation of bean failed", e);
        }
        return bean;
    }

    /**
     * 创建bean实例
     */
    protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
    
    
        Constructor usedConstructor = null;
        Constructor[] constructors = beanDefinition.getBeanClass().getDeclaredConstructors();

        for (Constructor ctor: constructors) {
    
    
            if (args != null && ctor.getParameterTypes().length == args.length) {
    
    
                Class[] parameterTypes = ctor.getParameterTypes();
                for (int i = 0; i < parameterTypes.length; i++) {
    
    
                    if (parameterTypes[i] != args[i].getClass()) {
    
    
                        break;
                    }
                }
                usedConstructor = ctor;
                break;
            }
        }
        return instantiation.instantiate(beanName, beanDefinition, usedConstructor, args);
    }

    /**
     * 属性填充
     */
    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) {
    
    
                    //  TODO 没有解决循环依赖问题
                    // 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);
        }
    }

    public void setInstantiation(InstantiationStrategy instantiation) {
    
    
        this.instantiation = instantiation;
    }
}

这个类的内容稍微有点长,主要包括三个方法:createBean、createBeanInstance、applyPropertyValues,这里我们主要关注 createBean 的方法中调用的 applyPropertyValues 方法。
在 applyPropertyValues 中,通过获取 beanDefinition.getPropertyValues() 并循环进行属性填充操作,如果遇到的是 BeanReference,那么就需要递归获取 Bean 实例,调用 getBean 方法。
当把依赖的 Bean 对象创建完成后,会递归回现在属性填充中。BeanUtil.setFieldValue(bean, name, value) 是 hutool-all 工具类中的方法,你也可以自己实现!这里需要注意我们并没有去处理循环依赖的问题,这部分内容较大,后续补充。

测试

事先准备

package com.qingyun.springframework.beansTest;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: 張青云
 * @create: 2021-08-19 00:13
 **/
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);
    }
}
package com.qingyun.springframework.beansTest;

/**
 * @description:
 * @author: 張青云
 * @create: 2021-08-18 22:54
 **/
public class UserService {
    
    
    private String uId;

    private UserDao userDao;

    public UserService() {
    
    
    }

    public void queryUserInfo() {
    
    
        System.out.println("查询用户信息:" + userDao.queryUserName(uId));
    }
}

测试用例

    public void beanTest3() {
    
    
        // 1.初始化 BeanFactory
        DefaultListableBeanFactory 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 注入bean
        BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues);
        beanFactory.registerBeanDefinition("userService", beanDefinition);

        // 5. UserService 获取bean
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.queryUserInfo();

    }
项目代码Github地址:https://github.com/Zhang-Qing-Yun/mini-spring,本节代码对应的commit标识为44c2179

欢迎标星

猜你喜欢

转载自blog.csdn.net/zhang_qing_yun/article/details/119792314