跳至主要內容

Spring 源码学习-Spring源码的阅读入口

科哒大约 3 分钟

1.阅读源码的注意点

  1. 有些类的方法为空,基本可以确定其具体实现逻辑由子类实现。
  2. 每加载到一个类,就有可能初识化一些值,创建一些预备对象。
  3. sping启动的时候分内置类和扩展类。
  4. 凡是带do*() 和 create*****()的方法,都是重点方法。**

2.ClassPathXmlApplicationContext(入口)

该步骤的主要操作内容是:

  • 初始化值
  • 加载操作系统属性
  • 获取启动文件

主方法

	/**
	 * Create a new ClassPathXmlApplicationContext with the given parent,
	 * loading the definitions from the given XML files.
	 * @param configLocations array of resource locations
	 * @param refresh whether to automatically refresh the context,
	 * loading all bean definitions and creating all singletons.
	 * Alternatively, call refresh manually after further configuring the context.
	 * @param parent the parent context
	 * @throws BeansException if context creation failed
	 * @see #refresh()
	 */
	public ClassPathXmlApplicationContext(
			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
			throws BeansException {
		// 调用父类构造方法,进行相关的对象创建等操作,包含属性的赋值操作
		super(parent);
		setConfigLocations(configLocations);
		if (refresh) {
			refresh();
		}
	}

点进super(parent) 一路F7


/**
* 使用给定的父上下文创建新的 AbstractApplicationContext。*
*/
public AbstractApplicationContext(@Nullable ApplicationContext parent) {
    this();
    setParent(parent);
}

程序执行到这一步


/**
* Create a new AbstractRefreshableApplicationContext with the given parent context.
* @param parent the parent context
*/
public AbstractRefreshableApplicationContext(@Nullable ApplicationContext parent) {
    super(parent);
}

这是上面提到的

每加载到一个类,就有可能初识化一些值,创建一些预备对象。

4.setConfigLocations

	/**
	 * 设置应用程序上下文的配置路径
	 *
	 * Set the config locations for this application context.
	 * <p>If not set, the implementation may use a default as appropriate.
	 */
	public void setConfigLocations(@Nullable String... locations) {
		if (locations != null) {
			Assert.noNullElements(locations, "Config locations must not be null");
			this.configLocations = new String[locations.length];
			for (int i = 0; i < locations.length; i++) {
				// 解析给定路径
				this.configLocations[i] = resolvePath(locations[i]).trim();
			}
		}
		else {
			this.configLocations = null;
		}
	}

5.resolvePath

解析资源路径

protected String resolvePath(String path) {
    return getEnvironment().resolveRequiredPlaceholders(path);
}

6.

****

@Override
    protected void customizePropertySources(MutablePropertySources propertySources) {
    propertySources.addLast(
    new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
propertySources.addLast(
    new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}

程序执行完setConfigLocations,我们可以看看到底放了什么进去

基本上都是****