Spring IOC(2)----如何注册bean定义

时间:2024-01-25 14:59:56

前面说到IOC容器在刷新之前的一些初始化工作,现在来看看在refresh()方法中,是怎样来加载注册我们自己的bean定义的。

refresh()方法中有很多功能,从注释中就可以看出来

 

我们本次重点关注invokeBeanFactoryPostProcessors(beanFactory);这个方法

该方法里面可以获取到前面初始化好的注解读取器AnnotatedBeanDefinitionReader,前面在AnnotationConfigUtils#registerAnnotationConfigProcessors方法中是往beanFactory里面注册了配置类的处理器的ConfigurationClassPostProcessor,这个时候就可以拿出来使用了,创建一个配置类解析器对象ConfigurationClassParser,来具体处理几种形式bean注入(@propertySource,@ComponentScan,@Import,@ImportResource,@bean),,最终再由ConfigurationClassBeanDefinitionReader将配置的bean定义加载到容器中ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForConfigurationClass

流程图如下: