Srping Framework源码阅读(一)-- Inversion of Control (IoC)初始化过程

一、结论

1. 目标结论

refresh方法是初始化Spring容器的核心代码,共分为12个步骤

2. 其他结论

设计思想:

  1. 抽象思维锻炼

代码风格:

  1. 代码应该有个好名字。起不了好名字,有可能是违反了单一职责
  2. javadoc文档。
  3. Spring源码的习惯utils使用abstract class,虽然平时大家都不会去new,但谁知道呢,“可能会/一定不会”

二、找线索

官方文档线索

官方文档摘录

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory.
The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.
Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.

这里说明我们应该关注org.springframework.beansorg.springframework.context两个包,核心接口BeanFactoryApplicationContext
可以初步确定研究对象 ClassPathXmlApplicationContextFileSystemXmlApplicationContext这两个实现类。

源码层线索

org.springframework.context包下ApplicationContext的实现
在这里插入图片描述
org.springframework.context包下创建IOC容器的方式:

  1. ClassPathXmlApplicationContext:从classpath中加载配置文件;
  2. FileSystemXmlApplicationContext:从磁盘中加载配置文件;
  3. AnnotationConfigApplicationContext:使用注解配置容器对象时,使用此类进行注解读取,创建容器;

三、源码分析

ClassPathXmlApplicationContext 类图

在这里插入图片描述

ClassPathXmlApplicationContext源码

	public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
   
		this(new String[] {
   configLocation}, true, null);
	}
	
	public ClassPathXmlApplicationContext(
			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
			throws BeansException {
   
		// ApplictionContext继承了HierarchicalBeanFactory,可以竖向竖向扩展
		super(parent);
		setConfigLocations(configLocations);
		if (refresh) {
   
			// 刷新IoC容器,这里调用的父类AbstractApplicationContext中的refresh实现
			refresh();
		}
	}

AbstractApplicationContext源码

	@Override
	public void refresh() throws BeansException, IllegalStat
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值