手写简易版Spring IOC容器01【学习】
文章目录
BeanDefinition
beanDefinition类
beanDefinition的作用主要是保存一个对象的类信息.
public class BeanDefinition {
private Class beanClass;
public BeanDefinition(Class beanClass){
this.beanClass = beanClass;
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
}
单例对象
单例对象注册(SingletonBeanRegistry)
public interface SingletonBeanRegistry {
Object getSingleton(String beanName);
}
DefaultSingletonBeanRegistry
单例对象保存的具体类,存入到map中,同时拥有get add单例的方法
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private Map<String, Object> singletonObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
protected void addSingleton(String beanName, Object singletonObject){
singletonObjects.put(beanName, singletonObject);
}
}
模板方法 BeanFactory
BeanFactory接口
定义了一个bean工厂拥有的基本方法
其中第一个代表无参生成bean,第二个代表有参生成bean
public interface BeanFactory {
public Object getBean(String name);
public Object getBean(String name,Object ...args);
}
AbstractBeanFactory 抽象工厂模板 (getBean)
继承单例 获得单例的全部方法,实现BeanFactory接口
然后定义模板方法和两个抽象方法让子类实现.
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
@Override
public Object getBean(String name) throws BeansException {
// 首先从单例对象的map中去获取.
Object bean = getSingleton(name);
if(bean != null) return bean;
// 去执行子方法的逻辑获取beanDefinition
BeanDefinition beanDefinition = getBeanDefinition(name);
return createBean(name,beanDefinition,null);
}
@Override
public Object getBean(String name, Object... args) {
// 首先从单例对象的map中去获取.
Object bean = getSingleton(name);
if(bean != null) return bean;
// 去执行子方法的逻辑获取beanDefinition
BeanDefinition beanDefinition = getBeanDefinition(name);
return createBean(name,beanDefinition,args);
}
protected abstract BeanDefinition getBeanDefinition(String beanName);
protected abstract Object createBean(String beanName,BeanDefinition beanDefinition,Object[] args) throws BeansException;
}
AbstractAutowireCapableBeanFactory (createBean 创建bean)
继承上一个抽象类实现其中模板定义的一个方法 (createBean)
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition,beanName,args);
}catch (Exception e){
e.printStackTrace();
}
addSingleton(beanName,bean);
return bean;
}
protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
Constructor constructorToUse = null;
Class<?> beanClass = beanDefinition.getBeanClass();
Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
for (Constructor ctor : declaredConstructors) {
if (null != args && ctor.getParameterTypes().length == args.length) {
constructorToUse = ctor;
break;
}
}
return instantiationStrategy.instantiate(beanDefinition, beanName, constructorToUse, args);
}
}
DefaultListableBeanFactory (getBeanDefinition) 获得bean定义
实现模板方法中bean定义的收集.
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {
private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
@Override
public BeanDefinition getBeanDefinition(String beanName) {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) throw new RuntimeException("发生了异常");
return beanDefinition;
}
// 注册bean.
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName, beanDefinition);
}
}
原文地址:https://blog.csdn.net/weixin_44794897/article/details/140526282
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!