[源码系列:手写spring] IOC第四节:Bean属性注入

主要内容

  • 添加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'}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值