引言
上篇末尾提到的,我们在HelloWorldService里看到了不和谐代码,为此,在我们的基础数据结构BeanDefinition
里新注入了实体PropertyValues,并在AutowireCapableBeanFactory里,有了applyPropertyValues()方法来管理
PropertyValue.
一言不合就上图:
UML图
类图:
时序图这里就不贴出来了,跟上篇博客的基本逻辑是类似的。
从上述的类图可以基本看出关键的步骤,BeanFactory和AbstractBeanFactory没变,而是子类
AutowireCapableBeanFactory适应需求,新增加了applyPropertyValues方法,而PropertyValue主要还是被
BeanDefinition管理着。下边看看有变化的代码:
BeanDefinition
package us.codecraft.tinyioc;
/**
* bean的内容及元数据,保存在BeanFactory中,包装bean的实体
* @author yihua.huang@dianping.com
*/
public class BeanDefinition {
private Object bean;
private Class beanClass;
private String beanClassName;
private PropertyValues propertyValues;
public BeanDefinition() {
}
public void setBean(Object bean) {
this.bean = bean;
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
public String getBeanClassName() {
return beanClassName;
}
public void setBeanClassName(String beanClassName) {
this.beanClassName = beanClassName;
try {
this.beanClass = Class.forName(beanClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Object getBean() {
return bean;
}
public PropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
}
PropertyValues
package us.codecraft.tinyioc;
import java.util.ArrayList;
import java.util.List;
/**
* 包装一个对象所有的PropertyValue。<br/>
* 为什么封装而不是直接用List?因为可以封装一些操作。
* @author yihua.huang@dianping.com
*/
public class PropertyValues {
private final List<PropertyValue> propertyValueList = new ArrayList<PropertyValue>();
public PropertyValues() {
}
public void addPropertyValue(PropertyValue pv) {
//TODO:这里可以对于重复propertyName进行判断,直接用list没法做到
this.propertyValueList.add(pv);
}
public List<PropertyValue> getPropertyValues() {
return this.propertyValueList;
}
}
PropertyValue
package us.codecraft.tinyioc;
/**
* 用于bean的属性注入
* @author yihua.huang@dianping.com
*/
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;
}
}
HelloWorldService
package us.codecraft.tinyioc;
/**
* @author yihua.huang@dianping.com
*/
public class HelloWorldService {
private String text;
public void helloWorld(){
System.out.println(text);
}
public void setText(String text) {
this.text = text;
}
}
BeanFactoryTest
package us.codecraft.tinyioc;
import org.junit.Test;
import us.codecraft.tinyioc.factory.AutowireCapableBeanFactory;
import us.codecraft.tinyioc.factory.BeanFactory;
/**
* @author yihua.huang@dianping.com
*/
public class BeanFactoryTest {
@Test
public void test() throws Exception {
// 1.初始化beanfactory
BeanFactory beanFactory = new AutowireCapableBeanFactory();
// 2.bean定义
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setBeanClassName("us.codecraft.tinyioc.HelloWorldService");
// 3.设置属性
PropertyValues propertyValues = new PropertyValues();
propertyValues.addPropertyValue(new PropertyValue("text", "Hello World!"));
beanDefinition.setPropertyValues(propertyValues);
// 4.生成bean
beanFactory.registerBeanDefinition("helloWorldService", beanDefinition);
// 5.获取bean
HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService");
helloWorldService.helloWorld();
}
}
总结
在BeanFactoryTest中,加载HelloWorldService还是用的最原始的方法,也还好有BeanClassName和BeanClass,
不然就尴尬了。所以,IoC在下一篇就要现身了哦~~~