【spring 阅读笔记】

Spring 源码阅读——ClassPathXmlApplicationtext 加载Bean过程

版本 sping5.2.7 springboot 2.2.8

项目结构图
Beans.xml 只有一个 bean 标签

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!--  scope="prototype" 原型模式 就是 new  默认singleton 单例"-->
    <bean id="helloWorld" class="com.example.my_code.spring.basics.pojo.HelloWorld">
        <property name="message" value="Hello World!"/>
    </bean>
</beans>

我的简单理解

  1. ApplicationContext 查看类关系
// An highlighted block
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
  	MessageSource, ApplicationEventPublisher, ResourcePatternResolver {....}

ListableBeanFactory, HierarchicalBeanFactory 这两个接口都继承 BeanFactory 这里我就简单认为 ApplicationContext是一个有很多额外功能的BeanFactory;

  1. ClassPathXmlApplicationContext(“Beans.xml”)
    个人认为应该分两个步骤 :一读取Beans.xml 二 解析Beans.xml 并初始化 bean到 查看其构造

     /**
   * Create a new ClassPathXmlApplicationContext, loading the definitions
   * from the given XML file and automatically refreshing the context.
   * @param configLocation resource location
   * @throws BeansException if context creation failed
   */
  public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
  	//内部代用如下
  	this(new String[] {configLocation}, true, null);
  }

public ClassPathXmlApplicationContext(
  		String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
  		throws BeansException {

  	super(parent);
  	setConfigLocations(configLocations);
  	if (refresh) {
  	//内部调用
  		refresh();
  	}
  }

这里我出现第一个其实在后面出现的疑惑
refresh()是 AbstractApplicationContext 类下的方法 查看类关系发现ClassPathXmlApplicationContext extends AbstractXmlApplicationContext
这里也即是子类继承父类也就是本地调用

refresh() 一切的开始

  	@Override
  public void refresh() throws BeansException, IllegalStateException {
  	synchronized (this.startupShutdownMonitor) {
  		// Prepare this context for refreshing.
  		prepareRefresh();
  		// Tell the subclass to refresh the internal bean factory.
  		//这里个就是最主要的实现我猜测的方法 加载 xml 并将xml 解析 注入到 Ben
  		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        ....
  }
  //本地调用 
  protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
  	refreshBeanFactory();
  	return getBeanFactory();
  }
  //继续调用本地的一个抽象方法 这里是我正真出现第一个疑惑的地方 他有两个实现类实现了方法 到底会调用哪一个?
  protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

AbstractRefreshableApplicationContext 和 GenericApplicationContext 都实现了 AbstractApplicationContext refreshBeanFactory() 方法 这是一个典型的模板方法 那么这里到底该调用哪一个方法 ,其实取决于调用者
在这里插入图片描述
由图可见 调用的是AbstractRefreshableApplicationContext.refreshBeanFactory() 方法 源码如下图

	@Override
	protected final void refreshBeanFactory() throws BeansException {
	     //当前存在BeanFactory 先关闭
		if (hasBeanFactory()) {
			destroyBeans();
			closeBeanFactory();
		}
		try {
		    //创建新的 beanFactory  
		    //DefaultListableBeanFactory 这个类里面存在所有BeanDefinition  相关数据
			DefaultListableBeanFactory beanFactory = createBeanFactory();
			beanFactory.setSerializationId(getId());
			customizeBeanFactory(beanFactory);
			//加载bean信息
			loadBeanDefinitions(beanFactory);
			this.beanFactory = beanFactory;
		}
		catch (IOException ex) {
			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
		}
	}

loadBeanDefinitions 调用 AbstractXmlApplicationContext

	@Override
	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
		// Create a new XmlBeanDefinitionReader for the given BeanFactory.
		//让XmlBeanDefinitionReader 解析读取Bean
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

		// Configure the bean definition reader with this context's
		// resource loading environment.
		beanDefinitionReader.setEnvironment(this.getEnvironment());
		beanDefinitionReader.setResourceLoader(this);
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

		// Allow a subclass to provide custom initialization of the reader,
		// then proceed with actually loading the bean definitions.
		initBeanDefinitionReader(beanDefinitionReader);
		loadBeanDefinitions(beanDefinitionReader);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值