Spring IOC容器启动过程

IOC容器启动过程

  1. 资源定位(classpath,filesystem等)
  2. 载入,将bean定义转换为内部数据结构BeanDefintion
  3. 向IOC容器注册bean

资源定位

定位配置文件,通过BeadDefinetionReader读入配置

refresh方法实现

IOC容器启动方法是ConfigurableApplicationContext中定义的refresh方法,在AbstractApplicationContext中实现

//准备阶段做的是:标记容器为active状态,以及检查当前的运行环境,比如使用log4j,还是jdklog等
prepareRefresh();

//创建beanfactory, 定位资源,载入,装配beandefinetion
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

//初始化beanfactory, 设置contex的classloader

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

try {
    //在基本的初始化之后,对beanfactory做一些修改
    // Allows post-processing of the bean factory in context subclasses.
    postProcessBeanFactory(beanFactory);

    //调用beanfactory后处理器
    // Invoke factory processors registered as beans in the context.
    invokeBeanFactoryPostProcessors(beanFactory);

    //注册bean后处理器
    // Register bean processors that intercept bean creation.
    registerBeanPostProcessors(beanFactory);

    //初始化消息资源,国际化
    // Initialize message source for this context.
    initMessageSource();

    // Initialize event multicaster for this context.
    initApplicationEventMulticaster();
    //在实例化bean之前,做一些工作
    // Initialize other special beans in specific context subclasses.
    onRefresh();

    //注册 ApplicationListener监听器
    // Check for listener beans and register them.
    registerListeners();

    //实例化bean
    // Instantiate all remaining (non-lazy-init) singletons.
    finishBeanFactoryInitialization(beanFactory);

    //发布bean正常响应事件,初始化bean生命周期处理器
    // Last step: publish corresponding event.
    finishRefresh();
}

BeanDefinetion

BeanDefinetion是定义bean的抽象

向IOC容器注册BeanDefinetion

用HashMap维护BeanDefinetion, beanName作为key,BeanDefintion作为value
如果有同名key,如果有配置overridebean,那就覆盖,没有就报错
populateBean方法完成bean的依赖注入

依赖注入

// Instantiate all remaining (non-lazy-init) singletons.
//在refresh方法中初始化非懒加载的bean
finishBeanFactoryInitialization(beanFactory);
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值