java程序入口

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

web程序入口

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

注意:不管上面哪种方式,最终都会调AbstractApplicationContext的refresh方法,而这个方法才是我们真正的入口。

流程解析

  • AbstractApplicationContextrefresh方法
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// STEP 1: 刷新预处理
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
// STEP 2:
// a) 创建IoC容器(DefaultListableBeanFactory)
// b) 加载解析XML文件(最终存储到Document对象中)
// c) 读取Document对象,并完成BeanDefinition的加载和注册工作
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
// STEP 3: 对IoC容器进行一些预处理(设置一些公共属性)
prepareBeanFactory(beanFactory); try {
// Allows post-processing of the bean factory in context subclasses.
// STEP 4:
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
// STEP 5: 调用BeanFactoryPostProcessor后置处理器对BeanDefinition处理
invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
// STEP 6: 注册BeanPostProcessor后置处理器
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
// STEP 7: 初始化一些消息源(比如处理国际化的i18n等消息源)
initMessageSource(); // Initialize event multicaster for this context.
// STEP 8: 初始化应用事件广播器
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
// STEP 9: 初始化一些特殊的bean
onRefresh(); // Check for listener beans and register them.
// STEP 10: 注册一些监听器
registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.
// STEP 11: 实例化剩余的单例bean(非懒加载方式)
// 注意事项:Bean的IoC、DI和AOP都是发生在此步骤
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
// STEP 12: 完成刷新时,需要发布对应的事件
finishRefresh();
} catch (BeansException ex) {
if (logger.isWarnEnabled()) {
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;
} finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}

【死磕 Spring】—— IoC 之深入理解 Spring IoC的更多相关文章

  1. 【死磕 Spring】----- IOC 之深入理解 Spring IoC

    在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...

  2. IOC 之深入理解 Spring IoC

    在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...

  3. 【死磕 Spring】

    [死磕 Spring]----- IOC 之深入理解 Spring IoC-------https://www.cnblogs.com/chenssy/p/9576769.html 1.Resourc ...

  4. Spring系列之谈谈对Spring IOC的理解

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IOC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  5. Spring系列(二):Spring IoC/DI的理解

    这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...

  6. (转)spring IOC、DI理解

    转自: http://www.cnblogs.com/xdp-gacl/p/4249939.html 个人理解: IOC控制反转,反转的是获取依赖对象的方式.传统的应用在存在依赖关系时,比如A依赖于B ...

  7. 深入理解Spring IOC

    转载自 http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概 ...

  8. Spring之旅第二篇-Spring IOC概念及原理分析

    一.IOC概念 上一篇已经了解了spring的相关概念,并且创建了一个Spring项目.spring中有最重要的两个概念:IOC和AOP,我们先从IOC入手. IOC全称Inversion of Co ...

  9. 深入理解Spring(一):初识Spring

    深入理解Spring(一):初识Spring 一. Spring介绍        Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnso ...

随机推荐

  1. C/C++中带可变参数的函数

    1.带可变参数的函数由来 当函数中的参数个数不确定时,这时候就需要带可变参数的函数! 如我们经常使用的C库函数printf()实际就是一个可变参数的函数, 其原型为: int printf( cons ...

  2. Asp.Net Grieview Eval 绑定数据 调用JS事件

    <asp:TemplateField ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp: ...

  3. vagrant 虚拟机中安装 lnamp 环境

    转载自 :http://git.oschina.net/apanly/mooc/tree/master/vagrant 替换源 sudo cp /etc/apt/sources.list /etc/a ...

  4. <亲测>阿里云centos7安装redis

    安装redis yum install redis 启动redis systemctl start redis.service 设置redis开机启动 systemctl enable redis.s ...

  5. PHP 中如何创建和修改数组?

    PHP中使用array来创建一个数组:array( key=>value , key=>value …… )用方括号的语法来修改数组:$arr[] = value 例如:$arr = ar ...

  6. WARNING: Package of target [javax.servlet.jsp.jstl.core.LoopTagSupport$1Status@7439e436] or package of member [public int javax.servlet.jsp.jstl.core.LoopTagSupport$1Status.getIndex()] are excluded!

    Struts2爆出045漏洞后,将struts版本升级到了2.3.32.但是在验证时发现有些jstl循环未出现预期的结果. debug发现,数据没有问题,断定是前端页面显示出了问题.根据日志信息WAR ...

  7. nump库的简单函数介绍

    1:ones函数(百度知道) ones()函数用以创建指定形状和类型的数组,默认情况下返回的类型是float64.但是,如果使用ones()函数时指定了数据类型,那么返回的就是该类型. 参考NumPy ...

  8. bzoj 4811: [Ynoi2017]由乃的OJ

    树链剖分,用zkw线段树维护每条链两个方向上对每一位的变换情况,由于位数较少,可以用两个unsigned long long表示 #include<cstdio> typedef unsi ...

  9. P2871 手链

    传送 这个题的数据限制没有翻译出来,所以有可能产生爆内存现象 再此翻译一下:1<=n(物品个数)<=3402,1<=M(总重量)<=12880 (就这两个有点用) 显然这是一个 ...

  10. window的三大功能,行内样式获取的讲解 getcomputerStyle

    window的三大功能: js引擎 dom渲染 获取行内中css的属性: chrome和Firefox加上 ie9+(所谓的高级浏览器)用的方法: window.getComeputerStyle(d ...