SSM框架搭建及源码解析-

发布时间:2017-3-31 22:21:58编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"SSM框架搭建及源码解析-",主要涉及到SSM框架搭建及源码解析-方面的内容,对于SSM框架搭建及源码解析-感兴趣的同学可以参考一下。

BeanPostProcessor接口是spring预留给bean创建过程中的扩展
bean的整个生命周期都是在调用BeanPostProcessor的实现类
这里写图片描述

执行InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation()

AbstractAutowireCapableBeanFactory的populateBean的createBean()

Object bean = resolveBeforeInstantiation(beanName, mbd);
    protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
        Object bean = null;
        if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
            // Make sure bean class is actually resolved at this point.
            if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
                Class<?> targetType = determineTargetType(beanName, mbd);
                if (targetType != null) {
                    bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                    if (bean != null) {
                        bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                    }
                }
            }
            mbd.beforeInstantiationResolved = (bean != null);
        }
        return bean;
    }

调用InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法,在构造方法执行之前扩展,如果扩展中会生成实例,则直接执行postProcessAfterInitialization()方法

执行Bean的构造器

AbstractAutowireCapableBeanFactory的createBean()

instanceWrapper = createBeanInstance(beanName, mbd, args);

执行MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition()

AbstractAutowireCapableBeanFactory的doCreateBean()

        synchronized (mbd.postProcessingLock) {
            if (!mbd.postProcessed) {
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                mbd.postProcessed = true;
            }
        }
        try {
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof MergedBeanDefinitionPostProcessor) {
                    MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
                    bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
                }
            }
        }

执行InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation()方法

AbstractAutowireCapableBeanFactory的populateBean()

        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                        continueWithPropertyPopulation = false;
                        break;
                    }
                }
            }
        }

执行InstantiationAwareBeanPostProcessor的postProcessPropertyValues()方法

在AbstractAutowireCapableBeanFactory类populateBean()

        if (hasInstAwareBpps || needsDepCheck) {
            PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
            if (hasInstAwareBpps) {
                for (BeanPostProcessor bp : getBeanPostProcessors()) {
                    if (bp instanceof InstantiationAwareBeanPostProcessor) {
                        InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                        pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                        if (pvs == null) {
                            return;
                        }
                    }
                }
            }
            if (needsDepCheck) {
                checkDependencies(beanName, mbd, filteredPds, pvs);
            }
        }

执行BeanPostProcessor的postProcessBeforeInitialization

在AbstractAutowireCapableBeanFactory类initializeBean()

        if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
        }
    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
            result = beanProcessor.postProcessBeforeInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }

执行Bean的init方法

invokeInitMethods(beanName, wrappedBean, mbd);

执行BeanPostProcessor的postProcessAfterInitialization

        if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
            result = beanProcessor.postProcessAfterInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }

执行DestructionAwareBeanPostProcessor的postProcessBeforeDestruction

        if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
            for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
                processor.postProcessBeforeDestruction(this.bean, this.beanName);
            }
        }

执行bean的destroy方法

        if (this.destroyMethod != null) {
            invokeCustomDestroyMethod(this.destroyMethod);
        }


上一篇:Python与人工神经网络(7)——其他正则化方法
下一篇:【正一专栏】没人相信你的时候,就是绝望的开始

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款