Spring

这篇博客详细介绍了Spring框架中ApplicationContext的初始化流程,从`new ClassPathXmlApplicationContext()`开始,逐步深入到`refresh()`方法,涉及资源配置、Bean工厂的准备、Bean的初始化等多个关键步骤,揭示了Spring应用上下文如何加载和管理Bean的全过程。
摘要由CSDN通过智能技术生成

new ClassPathXmlPathApplicationContext(“…”);

public ClassPathXmlApplicationContext(String configLocation) 
  throws BeansException
{
	this(new String[] {configLocation}, true, null);
}

this(new String[] {configLocation}, true, null);

// 使用给定的父级创建一个新的 ClassPathXmlApplicationContext,
//从给定的 XML 文件加载定义
public ClassPathXmlApplicationContext(String[] configLocations, 
                                      boolean refresh, 
                                      ApplicationContext parent)
  throws BeansException 
  {
	super(parent);
	setConfigLocations(configLocations);
	if (refresh) {
		refresh();
	}
}

super(parent);

紧接着一直调用上层,一直到 AbstractApplicationContext 的构造方法

public AbstractApplicationContext(ApplicationContext parent) {
	this();
	setParent(parent);
}

this();

public AbstractApplicationContext() {
	this.resourcePatternResolver = getResourcePatternResolver();
}

getResourcePatternResolver()

protected ResourcePatternResolver getResourcePatternResolver() {
	return new PathMatchingResourcePatternResolver(this);
}

// 紧接着,new PathMatchingResourcePatternResolver(this); 的代码如下:

// resourceLoader访问将通过线程上下文类加载器发生
public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
    Assert.notNull(resourceLoader, "ResourceLoader must not be null");
    this.resourceLoader = resourceLoader;
 }

setParent(parent);

@Override
public void setParent(ApplicationContext parent) {
	this.parent = parent;
	// 此时parent是null,所以不用往下看
	if (parent != null) {
		Environment parentEnvironment = parent.getEnvironment();
		if (parentEnvironment instanceof ConfigurableEnvironment) {
			getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
		}
	}
}

setConfigLocations(configLocations);

// 给应用程序设置资源的配置路径
// 如果没有设置,则可酌情使用默认值
public void setConfigLocations(String[] locations) {
	if (locations != null) {
		Assert.noNullElements(locations, "Config locations must not be null");
		this.configLocations = new String[locations.length];
		// 遍历locations数组,赋值给configLocations数组
		for (int i = 0; i < locations.length; i++) {
			this.configLocations[i] = resolvePath(locations[i]).trim();
		}
	}
	else {
		this.configLocations = null;
	}
}

refresh()

// 是否刷新上下文,加载所有的bean定义信息并创建所有的单例bean,
// 或者,在进一步配置上下文后手动调用refresh
if (refresh) {
	refresh();
}
// "refresh"和"destory"的同步监视器
private final Object startupShutdownMoitor = new Object();

// 由于这是一种启动方法,如果失败,它应该销毁已经创建的单例,以避免悬空资源。 
// 换句话说,在调用该方法之后,要么全部实例化,要么根本不实例化
@Override
public void refresh() throws BeansException, IllegalStateException {
	synchronized (this.startupShutdownMonitor) {
		// 准备上下文来刷新
		prepareRefresh();

		// 告诉子类刷新内部bean工厂
		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

		// Prepare the bean factory for use in this context.
		prepareBeanFactory(beanFactory);

		try {
			// Allows post-processing of the bean factory in context subclasses.
			postProcessBeanFactory(beanFactory);

			// Invoke factory processors registered as beans in the context.
			invokeBeanFactoryPostProcessors(beanFactory);

			// Register bean processors that intercept bean creation.
			registerBeanPostProcessors(beanFactory);

			// Initialize message source for this context.
			initMessageSource();

			// Initialize event multicaster for this context.
			initApplicationEventMulticaster();

			// Initialize other special beans in specific context subclasses.
			onRefresh();

			// Check for listener beans and register them.
			registerListeners();

			// Instantiate all remaining (non-lazy-init) singletons.
			finishBeanFactoryInitialization(beanFactory);

			// Last step: publish corresponding event.
			finishRefresh();
		}

		catch (BeansException ex) {
			logger.warn("Exception encountered during context initialization "
			   +"- cancelling refresh attempt", ex);

			// Destroy already created singletons to avoid dangling resources.
			destroyBeans();

			// Reset 'active' flag.
			cancelRefresh(ex);

			// Propagate exception to caller.
			throw ex;
		}
	}
}

prepareRefresh();

obtainFreshBeanFactory();

prepareBeanFactory(beanFactory);

postProcessBeanFactory(beanFactory);

invokeBeanFactoryPostProcessors(beanFactory);

registerBeanPostProcessors(beanFactory);

initMessageSource();

onRefresh();

registerListeners();

finishBeanFactoryInitialization(beanFactory);

finishRefresh();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值