文章目录
- new ClassPathXmlPathApplicationContext("...");
- this(new String[] {configLocation}, true, null);
- super(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;
}
}
}