用BeanDefinition
来描述bean
(spring的bean本质上就是Object),相比step-1可以明显的看出来,更加完整了。
public class BeanDefinition {
private Object bean;
private Class beanClass;
private String beanClassName;
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;
}
}
获取Class
类对象的三大方法:
1、MyObject x;
Class c1 = x.getClass();
2、Class c2=Class.forName(“MyObject”);
3、Class c3 = MyObject.class;
为Class
类创建一个实例:
x.getClass.newInstance()
BeanFactory
本质就是一个map,存放bean
的工厂,定义为接口。
public interface BeanFactory {
Object getBean(String name);
void registerBeanDefinition(String name, BeanDefinition beanDefinition);
}
AbstractBeanFactory
实现BeanFactory
的抽象类。
public abstract class AbstractBeanFactory implements BeanFactory{
private Map beanDefinitionMap = new ConcurrentHashMap();
//表示重写的方法,1、检查是否正确的重写父类方法;2、提示看代码的人,这是重写的方法
@Override
public Object getBean(String name) {
return beanDefinitionMap.get(name).getBean();
}
@Override
public void registerBeanDefinition(String name, BeanDefinition beanDefinition) {
Object bean = doCreateBean(beanDefinition);
beanDefinition.setBean(bean);
beanDefinitionMap.put(name, beanDefinition);
}
/**
* 初始化bean
* @param beanDefinition
* @return
*/
protected abstract Object doCreateBean(BeanDefinition beanDefinition);
}
AutowireCapableBeanFactory
继承AbstractBeanFactory
,实现初始化bean
的方法。
public class AutowireCapableBeanFactory extends AbstractBeanFactory {
@Override
protected Object doCreateBean(BeanDefinition beanDefinition) {
try {
Object bean = beanDefinition.getBeanClass().newInstance();
return bean;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
public class HelloWorld {
public void say(){
System.out.println("Hello World!");
}
}
public class test {
public static void main(String[] args) {
// 1.初始化beanfactory
AutowireCapableBeanFactory autowireCapableBeanFactory = new AutowireCapableBeanFactory();
// 2.注入bean
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setBeanClassName("com.zhudan.test2.demo3.HelloWorld");
autowireCapableBeanFactory.registerBeanDefinition("HelloWorld", beanDefinition);
// 3.获取bean
HelloWorld helloWorld = (HelloWorld) autowireCapableBeanFactory.getBean("HelloWorld");
helloWorld.say();
}
}
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;
}
}
public class PropertyValues {
private final List propertyValueList = new ArrayList();
public PropertyValues() {
}
public void addPropertyValue(PropertyValue pv) {
//为什么封装而不是直接用List? 因为可以封装一些操作。
//比如,这里可以对于重复propertyName进行判断,直接用list没法做到
this.propertyValueList.add(pv);
}
public List getPropertyValues() {
return this.propertyValueList;
}
}
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;
}
}
public class AutowireCapableBeanFactory extends AbstractBeanFactory {
@Override
protected Object doCreateBean(BeanDefinition beanDefinition) throws Exception {
Object bean = createBeanInstance(beanDefinition);
applyPropertyValues(bean, beanDefinition);
return bean;
}
protected Object createBeanInstance(BeanDefinition beanDefinition) throws Exception {
return beanDefinition.getBeanClass().newInstance();
}
protected void applyPropertyValues(Object bean, BeanDefinition mbd) throws Exception {
for (PropertyValue propertyValue : mbd.getPropertyValues().getPropertyValues()) {
Field declaredField = bean.getClass().getDeclaredField(propertyValue.getName());
declaredField.setAccessible(true);
declaredField.set(bean, propertyValue.getValue());
}
}
}
getFields()与getDeclaredFields()区别:getFields()只能访问类中声明为公有的字段,私有的字段它无法访问.getDeclaredFields()能访问类中所有的字段,与public,private,protect无关 。
public class HelloWorld {
private String text;
public void setText(String text) {
this.text = text;
}
public void say() {
System.out.println(text);
}
}
public class test {
public static void main(String[] args) throws Exception {
// 1.初始化beanfactory
BeanFactory beanFactory = new AutowireCapableBeanFactory();
// 2.bean定义
BeanDefinition beanDefinition = new BeanDefinition();
// 3.设置属性
PropertyValues propertyValues = new PropertyValues();
PropertyValue propertyValue = new PropertyValue("text", "HelloWorld!");
propertyValues.addPropertyValue(propertyValue);
// 4.注入bean
beanDefinition.setPropertyValues(propertyValues);
beanDefinition.setBeanClassName("com.zhudan.test2.demo4.HelloWorld");
beanFactory.registerBeanDefinition("hello", beanDefinition);
// 5.获取bean
HelloWorld helloWorld = (HelloWorld) beanFactory.getBean("hello");
helloWorld.say();
}
}
Spring源码自然比上面的项目要复杂得多,所以我一般主要看方法和类的继承关系。
public interface PropertyValues extends Iterable