转载

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

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

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

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

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

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

流程图如下:

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

原文  http://www.cnblogs.com/nijunyang/p/11631724.html
正文到此结束
Loading...