SpringContext的创建过程分析

在Spring框架中IOC容器起到了关键作用,Context模块也是整个Spring框架当之无愧的根基。每个Spring应用程序都至少存在一个Context对象,Context的创建过程中具体进行了什么操作呢,下面以最常用的AnnotationConfigApplicationContext为例,来说明SpringContext的创建过程,以下为示例代码:

//配置类
@Configuration
public class AnnoBeanConfig {
    @Bean
    public BeanPostProcessor getPostProcessorBean() {return new PostProcessorBean();}

    @Bean(name = "Anno-Bean", initMethod = "initFunc", destroyMethod = "destroyFunc")
    @Scope("singleton")//作用域
    @Lazy//懒加载
    public AnnotationBean getAnnotationBean() {
        AnnotationBean bean = new AnnotationBean();
        bean.setMessage("基于注解的配置");
        return bean;
    }

    @Bean(name = "Simple-Bean", initMethod = "initFunc", destroyMethod = "destroyFunc")
    public SimpleBean getSimpleBean() {
        SimpleBean bean = new SimpleBean();
        bean.setMessage("实现初始化回调和销毁回调方法");
        return bean;
    }
}

//测试类
public class AnnoBeanFactoryTest {
    AnnotationConfigApplicationContext context=null;

    @Before
    public void doBefore(){
        this.context=new AnnotationConfigApplicationContext(AnnoBeanConfig.class);
    }
  
  	@After
    public void doAfter(){
        this.context.registerShutdownHook();
    }

    @Test
    public void testAnnoBean(){
        AnnotationBean bean = (AnnotationBean) context.getBean("Anno-Bean");
        PrintTool.printBeanInfo(bean);
    }
}

一、Context基本创建过程

AnnotationConfigApplicationContext类的构造方法分三个步骤执行,调用无参构造、注册配置类register(componentClasses);、调用父类AbstractApplicationContextrefresh();方法。其中,无参构造方法执行完成后 beanDefinedMap属性中只有5条内容,分别是:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor -> {RootBeanDefinition@1226} 
org.springframework.context.event.internalEventListenerFactory -> {RootBeanDefinition@1228} 
org.springframework.context.event.internalEventListenerProcessor -> {RootBeanDefinition@1230} 
org.springframework.context.annotation.internalAutowiredAnnotationProcessor -> {RootBeanDefinition@1232} 
org.springframework.context.annotation.internalCommonAnnotationProcessor -> {RootBeanDefinition@1234} 

四个类对象, register(componentClasses);方法调用完成后,beanDefinedMap属性中增加了1条或多条,数量取决于创建AnnotationConfigApplicationContext对象时传入的参数数量,以本例中的参数为例,新增的内容为:

annoBeanConfig -> {AnnotatedGenericBeanDefinition@1395} 

refresh();方法执行完成后,beanDefinedMap属性中增加了3条信息(数量取决于配置类中@Bean的数量),新增的内容为:

Anno-Bean -> {ConfigurationClassBeanDefinitionReader$ConfigurationClassBeanDefinition@1916} 
Simple-Bean -> {ConfigurationClassBeanDefinitionReader$ConfigurationClassBeanDefinition@1918} 
getPostProcessorBean -> {ConfigurationClassBeanDefinitionReader$ConfigurationClassBeanDefinition@1914} 

由此可见,在这三个对象中的key正好对应配置类中的@Bean注解,如果没有设置Bean的名称,则默认使用方法名为Bean的名称。

二、Context具体创建过程

register()方法

register()方法用于创建配置类对象的Bean,此方法执行时序图如下:
register()方法时序图

在这个过程中,register()方法需要传入配置类对象,内部首先调用Assert.notEmpty()方法验证配置类对象是否存在,之后调用reader属性的register()方法,reader对象的registerBean()方法和doRegisterBean()方法不执行任何操作,最后在BeanDefinitionReaderUtils.registerBeanDefinition()方法中调用contextregisterBeanDefinition()方法完成配置类对象的注册。

注意:Spring的配置类本身也是容器中的一个Bean

refresh()方法

refresh()方法是创建Bean的方法,在AbstractApplicationContext类中定义,是一个模板方法,具体的实现类可以通过重写钩子方法对此方法进行一定程度上的修改,此方法源码如下:

@Override
public void refresh() throws BeansException, IllegalStateException {
  synchronized (this.startupShutdownMonitor) {
    //准备工作
    prepareRefresh();

    //创建内部BeanFactory对象
    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

    //设定BeanFactory
    prepareBeanFactory(beanFactory);

    try {
      //钩子方法1
      postProcessBeanFactory(beanFactory);

      //调用工厂处理器,创建Bean对象定义
      invokeBeanFactoryPostProcessors(beanFactory);

      //注册Bean的后置处理器,创建Bean对象
      registerBeanPostProcessors(beanFactory);

      //初始化消息代码
      initMessageSource();

      //为容器初始化事件多路广播器
      initApplicationEventMulticaster();

      //钩子方法2:用于创建特殊的Bean
      onRefresh();

      //注册监听Bean
      registerListeners();

      //完成Bean工厂初始化,实例化所有非懒加载的单例Bean
      finishBeanFactoryInitialization(beanFactory);

      //发布事件
      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();
    }
  }
}

invokeBeanFactoryPostProcessors()方法执行过程中,创建配置类中配置的Bean对象定义,保存到beanDefinitionMap中,通过debug可以看出,此方法执行完成后,beanDefinitionMap中增加了三条信息,size的值由 6 增加到 9 ,但是singletonObjects对象中并没有配置类Bean对象和配置类中配置的Bean对象。registerBeanPostProcessors()方法执行后,singletonObjects对象中才存在了配置类对象和部分Bean,在此测试代码中是getPostProcessorBeanannoBeanConfig,而Simple-Bean对象是在finishBeanFactoryInitialization()方法中创建的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李奇技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值