Spring框架——关于IOC容器,Bean的生命周期创建源码全过程解读

流程

1——传入配置类创建IOC容器

AnnotationConfigApplicationContext applicationContext = new        AnnotationConfigApplicationContext(配置类.class);

2——注册配置类,调用refresh方法()刷新容器 

public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
    this();
    this.register(annotatedClasses);
    this.refresh();
}

3——注册Bean的后置处理器,来拦截Bean的创建

this.registerBeanPostProcessors(beanFactory);

3.1后置处理器的注册逻辑为:

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

 3.2先拿到所有IOC容器中我们需要的后置处理器

String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

3.3优先注册priorityOrderedPostProcessors接口的BeanPostProcessor

sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, (List)priorityOrderedPostProcessors);

3.4 然后再给容器中注册Ordered接口的BeanPostprocessor

sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, (List)orderedPostProcessors);

3.5 最后给没实现优先级接口的BeanPostcessor

registerBeanPostProcessors(beanFactory, (List)nonOrderedPostProcessors);
sortPostProcessors(internalPostProcessors, beanFactory);

 

四——调用Doget Bean方法,获得单实例bean(第一次获取不到)

if (mbd.isSingleton()) {
    sharedInstance = this.getSingleton(beanName, new ObjectFactory<Object>() {
        public Object getObject() throws BeansException {
            try {
                return AbstractBeanFactory.this.createBean(beanName, mbd, args);
            } catch (BeansException var2) {
                AbstractBeanFactory.this.destroySingleton(beanName);
                throw var2;
            }
        }
    });

 

五——注册BeanPostProcessor,实际上就是创建 BeanPostProcessor对象,保存在容器中

5.1创建internalAutoProxyCreator的BeanPostProcessor

 sharedInstance = this.getSingleton(beanName, new ObjectFactory<Object>()

 5.2创建Bean的实例

BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
    instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName);
}

if (instanceWrapper == null) {
    instanceWrapper = this.createBeanInstance(beanName, mbd, args);
}

5.3 通过populateBean给Bean的属性赋值,通过initializeBean给Bean初始化

Object exposedObject = bean;

try {
    this.populateBean(beanName, mbd, instanceWrapper);
    if (exposedObject != null) {
        exposedObject = this.initializeBean(beanName, exposedObject, mbd);
    }

六——具体关于如果initializeBean初始化解读 

6.1调用this.invokeAwareMethods(beanName, bean)方法,处理Aware接口的方法回调

6.2applyBeanPostProcessorsBeforeInitialization,应用后置处理器的BeforePostProcessor

Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
    wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
}

6.3执行初始化方法

this.invokeInitMethods(beanName, wrappedBean, mbd);

6.4 执行后置处理器的AfterInitialization方法

if (mbd == null || !mbd.isSynthetic()) {
    wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}

 七——正常执行invokeAwareMethods方法,实现了BeanFactoryAware方法

public void setBeanFactory(BeanFactory beanFactory) {
    super.setBeanFactory(beanFactory);
    if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
        throw new IllegalArgumentException("AdvisorAutoProxyCreator requires a ConfigurableListableBeanFactory: " + beanFactory);
    } else {
        this.initBeanFactory((ConfigurableListableBeanFactory)beanFactory);
    }
}

 初始化BeanFactory

this.initBeanFactory((ConfigurableListableBeanFactory)beanFactory);

 调用父类的SetBeanFactory方法

public void setBeanFactory(BeanFactory beanFactory) {
    this.beanFactory = beanFactory;
}

在调用initBeanFactory方法

protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    super.initBeanFactory(beanFactory);
    if (this.aspectJAdvisorFactory == null) {
        this.aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory(beanFactory);
    }this.aspectJAdvisorsBuilder = new AnnotationAwareAspectJAutoProxyCreator.BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory);

创建成功BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator——>AspectJAdvisorFactory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值