`

Spring源码阅读:XmlWebApplicationContext

阅读更多
XmlWebApplicationContext是spring在web应用中的applicationContext,
在ContextLoaderListener或ContextLoaderServlet中完成了初步的设置。

XmlWebApplicationContext跟其他的applicationContext(FileSystem,Classpath)一样,是AbstractApplicationContext的一个子类。
applicationContext的refresh过程是线程同步的。
如下:
synchronized (this.startupShutdownMonitor)

阅读笔记:

方法1
prepareRefresh();  //设定起始时间,并设置active标记

方法2
prepareBeanFactory(beanFactory);

// 完成BeanFactory的创建
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {
            logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
        }
        return beanFactory;
    }


refreshBeanFactory 在AbstractRefreshableApplicationContext(是XmlWebApplicationContext的父类)中进行了实现。
创建了DefaultListableBeanFactory,随后执行loadBeanDefinitions(beanFactory)。
具体的加载过程是委托给XmlBeanDefinitionReader完成。

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
        loadBeanDefinitions(beanDefinitionReader);
}


该方法中的XmlBeanDefinitionReader完成了WEB-INF/applicationContext.xml(可指定contextConfigLoaction的值,可使用,; \t\n指定多个applicationContext.xml文件)的加载

// 获取beanDefinitionConfig,循环加载
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
                // 完成所有applicationContext.xml的加载
                reader.loadBeanDefinitions(configLocation);
}
}
}


方法3
下面的一大片,慢慢看吧。

        beanFactory.setBeanClassLoader(getClassLoader());
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));

// 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();
分享到:
评论

相关推荐

    spring-learning:我的学习Spring和Spring Boot的笔记

    Spring学习 此存储库包含有关对Spring Framework(v5.2.x)和Spring Boot(v2.4.x)的源代码进行分析的学习笔记。... ApplicationContext applicationContext = new XmlWebApplicationContext ( new ClasspathR

    spring源代码解析

    而一般的启动过程,Spring会使用一个默认的实现,XmlWebApplicationContext – 这个上下文实现作为在web容器中的根上下文容器被建立起来,具体的建立过程在下面我们会详细分析。 Java代码 public class ...

    spring-web-2.5.jar

    org.springframework.web.context.support.XmlWebApplicationContext.class org.springframework.web.filter.AbstractRequestLoggingFilter.class org.springframework.web.filter.CharacterEncodingFilter.class ...

    XmlWebApplicationContext的Resource定位时序图

    XmlWebApplicationContext的Resource定位时序图啊啊啊啊

    Spring中ApplicationContext加载机制

    XmlWebApplicationContext:专为 Web 工程定制的,例如: ```java ApplicationContext ctx = new XmlWebApplicationContext("WEB-INF/applicationContext.xml"); ``` 这些实现都可以根据实际情况进行选择,并且...

    Spring的监听器ContextLoaderListener的作用

    ContextLoaderServlet 实现了 HttpServlet 接口,负责创建 XmlWebApplicationContext 这个类,它实现的接口是 WebApplicationContext -> ConfigurableWebApplicationContext -> ApplicationContext -> BeanFactory。...

    SPRING API 2.0.CHM

    All Classes AbstractAdvisorAutoProxyCreator AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory....

Global site tag (gtag.js) - Google Analytics