栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring

文章目录
  • new ClassPathXmlPathApplicationContext("...");
  • this(new String[] {configLocation}, true, null);
  • super(parent);
    • this();
      • getResourcePatternResolver()
    • setParent(parent);
  • setConfigLocations(configLocations);
  • refresh()
    • prepareRefresh();
    • obtainFreshBeanFactory();
    • prepareBeanFactory(beanFactory);
    • postProcessBeanFactory(beanFactory);
    • invokeBeanFactoryPostProcessors(beanFactory);
    • registerBeanPostProcessors(beanFactory);
    • initMessageSource();
    • onRefresh();
    • registerListeners();
    • finishBeanFactoryInitialization(beanFactory);
    • finishRefresh();

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();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/336697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号