③. 源码Debug总结
- ①. 源码步骤:
源码步骤: //1.先传入一个bean的xml文件 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //2.这个xml配置文件,根据它来创建ioc容器 public ClassPathXmlApplicationContext(String configLocation) throws BeansException { this(new String[] {configLocation}, true, null); } //3.ioc容器底层还要刷新容器 if (refresh) { refresh(); } //4.刷新容器的时候。它得先创建bean的工厂 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); //5.创建bean工厂的时候(也叫刷新工厂) protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { refreshBeanFactory(); return getBeanFactory(); } //6.//加载所有的bean定义信息 DefaultListableBeanFactory beanFactory = createBeanFactory(); //创建保存所有bean定义信息的档案馆 beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); this.beanFactory = beanFactory; //7.beanDefinitionReader去读取传入的xml配置文件的内容() loadBeanDefinitions(beanDefinitionReader); //8.读取指定的配置文件 if (configLocations != null) { //读取器来加载所有的bean定义信息,也就是相当于读取器从这一行开始去读取配置文件了 reader.loadBeanDefinitions(configLocations); } //9.读取器挨个遍历每一个指定的配置文件去来解析 count += loadBeanDefinitions(location); //10.加载指定配置文件的所有内容 @Override public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException { return loadBeanDefinitions(location, null); //加载bean的定义信息,(参数一:配置文件传过来即可) } //11.利用DocumentReader开始读取整篇文档 public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException { BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader(); int countBefore = getRegistry().getBeanDefinitionCount(); documentReader.registerBeanDefinitions(doc, createReaderContext(resource)); return getRegistry().getBeanDefinitionCount() - countBefore; } //12.处理bean的定义信息 parseBeanDefinitions(root, this.delegate); //13.dom解析当前标签生成BeanDefinitionHolder @Nullable public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) { String id = ele.getAttribute(ID_ATTRIBUTE); String nameAttr = ele.getAttribute(NAME_ATTRIBUTE); //14.准备创建一个抽象的bean定义信息 AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean); //15.bean定义信息的类名就是xml配置文件指定写的类名 GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setParentName(parentName); if (className != null) { if (classLoader != null) { bd.setBeanClass(ClassUtils.forName(className, classLoader)); }
②. 图像展示