28

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

 4 years ago
source link: https://www.tuicool.com/articles/32Yv6nj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

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

q6zQ7fm.png!web

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

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

流程图如下:

QFNfAnU.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK