主要内容
添加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'}