In-depth understanding SpringIOC (a) --- Ioc container initialization process

introduction:

What is the IOC container?

beanDefinitionMap, singletonObjects (singleton buffer pool), PostProcessor (post processor) to complete the series springIOC this core functionality. And it refers not only singletonObjects (singleton buffer pool).

Ioc container initialization process

When we use the Spring framework in the application, we need to get a spring context, and derive bean we need. So spring is how to help us to create objects of it? Let's take a look at ioc container initialization process

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(simpleConfig.class);

ctx.getBean("cat");

But after we use spring framework, only you need to configure the application in a series of notes (XML also fine, but we should all notes of the bar), and then write the above line of code in the Main method, get a spring context, and then you can get the objects we want by getBean () method.

Let us enter AnnotationConfigApplicationContext (simpleConfig.class) to see how ioc container is initialized.

Here Insert Picture Description

IOC initialization flowchartHere Insert Picture Description

this()

public AnnotationConfigApplicationContext() {

   //初始化注解模式下的Bean定义扫描器
   this.reader = new AnnotatedBeanDefinitionReader(this);
   //初始化ClassPath类型的Bean定义扫描器
   this.scanner = new ClassPathBeanDefinitionScanner(this);
}

register(annotatedClasses)

Spring action will register six configuration and processing of the class must be instantiated and class Configuration annotations placed in beanDefinitionMap
Here Insert Picture Description

refresh()

public void refresh() throws BeansException, IllegalStateException {
   Object var1 = this.startupShutdownMonitor;
   synchronized(this.startupShutdownMonitor) {

      //刷新容器前的准备(开始时间/启动标志等)
      this.prepareRefresh();

      //刷新BeanFactory
      ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();

      //配置类的标准属性(类加载器之类的)
      this.prepareBeanFactory(beanFactory);

      try {
      
        /*
	* spring中并没有具体去实现postProcessBeanFactory方法,是提供给想要实现BeanFactoryPostProcessor的三方框架使用的。谁要使用谁就去        实现。
	* 作用是在BeanFactory准备工作完成后做一些定制化的处理,实现BeanPostProcessor接口的使用,注入一些重要资源,或修改Bean的行为,
	* 得到beanFactory,为所欲为!
	*/
          this.postProcessBeanFactory(beanFactory);

         //找到所有的BeanFactoryPostProcessor并执行他们的方法
         /*
          原理:从容器中获取获取到所有的BeanDefinitionRegistryPostProcessor组件,然后依次触发所有的postProcessBeanDefinitionRegistry()方法,
         再来从容器中找到BeanFactoryPostProcessor组件,然后依次触发所有的postProcessBeanFactory()方法。
        */         
         this.invokeBeanFactoryPostProcessors(beanFactory);

         //注册BeanPostProcessor
         this.registerBeanPostProcessors(beanFactory);

         //初始化initMessageSource组件,用于国际化等
         this.initMessageSource();

         //初始化自定义事件广播器
         this.initApplicationEventMulticaster();

         //初始化其它的特殊bean(子类增强器)
         this.onRefresh();

         //注册监听器
         this.registerListeners();

         //初始化Spring单例Bean  ----》完成bean的一系列装配
         this.finishBeanFactoryInitialization(beanFactory);

         //完成初始化,发布初始化事件
         this.finishRefresh();
      } catch (BeansException var9) {
         if (this.logger.isWarnEnabled()) {
            this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
         }

         this.destroyBeans();
         this.cancelRefresh(var9);
         throw var9;
      } finally {
         this.resetCommonCaches();
      }

   }
}

Think

1. Why create DefaultListableBeanFactory and not some other BeanFactory?

Here Insert Picture Description
Inherits all the BeanFactory, and most functional.

2. The relationship between BeanFactory and the ApplicationContext?

Combination of relationship, ApplicationContext BeanFactory on the basis of a lot of extended features
Here Insert Picture Description

Published 45 original articles · won praise 3 · Views 2312

Guess you like

Origin blog.csdn.net/weixin_44046437/article/details/99763395