Spring系列(三) 从refresh看createBean源码解析

createBean解析

spring当中单例bean的创建原理,其中涉及到了非常多阶段及各种处理器的调用

启动代码

通过注解配置类AppConfig启动一个容器,其中添加了一个自定义的beanFactoryPostProcessor, 注意:这个类是直接添加到beanFactoryPostProcessors中的,没有被spring当成bean放到单例map中

 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
        ac.addBeanFactoryPostProcessor(new CustomBeanFactoryPostProcessor());
        ac.register(AppConfig.class);
        ac.refresh();
new AnnotationConfigApplicationContext

完成的初始化:

this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);

初始化了AnnotatedBeanDefinitionReader又完成了spring内置几大处理器的注册

AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);

生成了对应RootBeanDefinition,并通过registry加到了map中

beanName beanClass
org.springframework.context.annotation.internalConfigurationAnnotationProcessor ConfigurationClassPostProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor AutowiredAnnotationBeanPostProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor CommonAnnotationBeanPostProcessor
org.springframework.context.event.internalEventListenerProcessor EventListenerMethodProcessor
org.springframework.context.event.internalEventListenerFactory DefaultEventListenerFactory
register 注册了自己的配置类

通过构造ApplicationContext时产生的reader解读这个注解类,并把配置类当成一个AnnotatedGenericBeanDefinition放入map中等待后续实例化。中间处理了一些common的注解信息并填冲到bd里,比如@Lazy,@Primary,@Role,@DependsOn, @Description等

BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
refresh 方法解析

refresh方法中是个模板方法,里面定义了很多阶段操作,其中主要分析以下几个

invokeBeanFactoryPostProcessors(beanFactory)

主要完成了下面一个操作,其中getBeanFa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值