spring IOC之篇六 bean的加载---bean的创建

之前我们讲解了缓存中单例的获取,解下来需要讲解缓存中不存在该如何处理

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
        Assert.notNull(beanName, "‘beanName‘ must not be null");
        // 全局变量需要同步
        synchronized (this.singletonObjects) {
           // 先从单例缓存池获取,以便于复用
            Object singletonObject = this.singletonObjects.get(beanName);
            if (singletonObject == null) {
               // 如果为空,才进行单例bean的初始化
                if (this.singletonsCurrentlyInDestruction) {
                    throw new BeanCreationNotAllowedException(beanName,
                            "Singleton bean creation not allowed while the singletons of this factory are in destruction " +
                            "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Creating shared instance of singleton bean ‘" + beanName + "‘");
                }
               //记录加载状态,this.singletonsCurrentlyInCreation.add(beanName),把正在创建的Bean加入到缓存中,可以对循环依赖进行检测
                beforeSingletonCreation(beanName);
                boolean newSingleton = false;
                boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = new LinkedHashSet<Exception>();
                }
                try {
                    //初始化bean
                    singletonObject = singletonFactory.getObject();
                    newSingleton = true;
                }
                catch (IllegalStateException ex) {
                    // Has the singleton object implicitly appeared in the meantime ->
                    // if yes, proceed with it since the exception indicates that state.
                    singletonObject = this.singletonObjects.get(beanName);
                    if (singletonObject == null) {
                        throw ex;
                    }
                }
                catch (BeanCreationException ex) {
                    if (recordSuppressedExceptions) {
                        for (Exception suppressedException : this.suppressedExceptions) {
                            ex.addRelatedCause(suppressedException);
                        }
                    }
                    throw ex;
                }
                finally {
                    if (recordSuppressedExceptions) {
                        this.suppressedExceptions = null;
                    }
                 // 移除缓存中正在加载bean的状态
                    afterSingletonCreation(beanName);
                }
                if (newSingleton) {
                    // 将结果记录值缓存,并删除所有的辅助状态
                    addSingleton(beanName, singletonObject);
                }
            }
            return (singletonObject != NULL_OBJECT ? singletonObject : null);
        }
    }

接下来对代码 singletonObject = this.singletonObjects.get(beanName);进行分析

我们跟踪了这么久的代码发现,一个真正干活的代码一般是以do开头的,比如 doGetObejctFactoryBean,而给我们错觉的函数,比如getObejctFactoryBean 只是做一些全局的统筹工作,这个规则对createBean也不列外。

@Override
    protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
        if (logger.isDebugEnabled()) {
            logger.debug("Creating instance of bean ‘" + beanName + "‘");
        }
        RootBeanDefinition mbdToUse = mbd;

        // Make sure bean class is actually resolved at this point, and
        // clone the bean definition in case of a dynamically resolved Class
        // which cannot be stored in the shared merged bean definition.
       // 根据设置的class来解析class
        Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
        if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
            mbdToUse = new RootBeanDefinition(mbd);
            mbdToUse.setBeanClass(resolvedClass);
        }

        // Prepare method overrides.
        try {
        // 对 override-method进行验证以及标记,其实是指的lookup-method和replace-method属性的配置相关的信息,是统一放到BeanDefinition的methodOverrides属性里面
            mbdToUse.prepareMethodOverrides();
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                    beanName, "Validation of method overrides failed", ex);
        }

        try {
            // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.     // 应用初始化后处理器,解析是否存在初始化前的短路操作,如果是AOP则,bean不为空,直接返回
                     Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
            if (bean != null) {
                return bean;
            }
        }
        catch (Throwable ex) {
            throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                    "BeanPostProcessor before instantiation of bean failed", ex);
        }
      //创建bean
        Object beanInstance = doCreateBean(beanName, mbdToUse, args);
        if (logger.isDebugEnabled()) {
            logger.debug("Finished creating instance of bean ‘" + beanName + "‘");
        }
        return beanInstance;
    }

进一步的对createBean进行分析

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
        // Instantiate the bean.
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
          // 将BeanDefinition转换为BeanWrapper,以便于在装配的时候把属性值都赋值到BeanWrapper对象里面,BeanWrapper实现了属性编辑器接口,会把默认对象的属性编辑对象注册到PropertyEditorRegistrySupport对象的属性中去,方便遇到对应的属性值的进行解析
            instanceWrapper = createBeanInstance(beanName, mbd, args);
        }
        final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
        Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);

        // Allow post-processors to modify the merged bean definition.
        synchronized (mbd.postProcessingLock) {
            if (!mbd.postProcessed) {
            // bean合并后的处理,Autowried正是基于此注解实现了类型的预解析
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                mbd.postProcessed = true;
            }
        }

        // Eagerly cache singletons to be able to resolve circular references
        // even when triggered by lifecycle interfaces like BeanFactoryAware.
        // spring的循环依赖问题的解决,当A中含有B属性时候,而B中有含有A属性时候就会构成一个循环依赖,此时如果A和B都是单列,那么Spring的处理方式就是当创建B的时候,如果涉及到A,则不去创建A而是通过放入缓存的ObectFactory来创建实例,因为属性的地址是一样的,这样就解决了循环依赖的问题。
        boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
                isSingletonCurrentlyInCreation(beanName));
        if (earlySingletonExposure) {
            if (logger.isDebugEnabled()) {
                logger.debug("Eagerly caching bean ‘" + beanName +
                        "‘ to allow for resolving potential circular references");
            }
 // 避免后期循环依赖,将bean的ObjectFactory对象加入到缓存中去
            addSingletonFactory(beanName, new ObjectFactory<Object>() {
                @Override
                public Object getObject() throws BeansException {
                    return getEarlyBeanReference(beanName, mbd, bean);
                }
            });
        }

        // Initialize the bean instance.
        Object exposedObject = bean;
        try {
           // 对bean进行装配,将各个属性值赋值注入,可以依赖其他bean则递归调用初始依赖bean
            populateBean(beanName, mbd, instanceWrapper);
            if (exposedObject != null) {
                // 调用初始化方法,如:init-method 如果实现了接口 InitializingBean,则会先调用afterPropertiesSet方法 之后会调用init-method方法
                exposedObject = initializeBean(beanName, exposedObject, mbd);
            }
        }
        catch (Throwable ex) {
            if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
                throw (BeanCreationException) ex;
            }
            else {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
            }
        }
       // 循环依赖检查,非单例情况下面,直接抛出异常
        if (earlySingletonExposure) {
            Object earlySingletonReference = getSingleton(beanName, false);
            if (earlySingletonReference != null) {
                if (exposedObject == bean) {
                    exposedObject = earlySingletonReference;
                }
                else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                    String[] dependentBeans = getDependentBeans(beanName);
                    Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
                    for (String dependentBean : dependentBeans) {
                        if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                            actualDependentBeans.add(dependentBean);
                        }
                    }
                    if (!actualDependentBeans.isEmpty()) {
                        throw new BeanCurrentlyInCreationException(beanName,
                                "Bean with name ‘" + beanName + "‘ has been injected into other beans [" +
                                StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                                "] in its raw version as part of a circular reference, but has eventually been " +
                                "wrapped. This means that said other beans do not use the final version of the " +
                                "bean. This is often the result of over-eager type matching - consider using " +
                                "‘getBeanNamesOfType‘ with the ‘allowEagerInit‘ flag turned off, for example.");
                    }
                }
            }
        }

        // Register bean as disposable.
        try {
         // 根据scope注册bean,包括销毁方法的注册 destory-method方法
            registerDisposableBeanIfNecessary(beanName, bean, mbd);
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
        }

        return exposedObject;
    }

原文地址:https://www.cnblogs.com/histlyb/p/8994242.html

时间: 2024-10-06 04:42:14

spring IOC之篇六 bean的加载---bean的创建的相关文章

spring注解第03课 按条件加载Bean @Conditional

package com.atguigu.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import com.atguigu.bean.Person; import com.atguigu

spring IOC之篇五 bean的加载

我们终于结束了对XML配置文件的解析,接下来我们要分析bean的加载,即对代码getBean的分析: People p = (People)ctx.getBean("cutesource"); @SuppressWarnings("unchecked") protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args,

【死磕 Spring】----- IOC 之 加载 Bean

先看一段熟悉的代码: ClassPathResourceresource new ClassPathResource("bean.xml");DefaultListableBeanFactoryfactory new DefaultListableBeanFactory();XmlBeanDefinitionReaderreader new XmlBeanDefinitionReader(factory);reader.loadBeanDefinitions(resource);这段代

spring源码深度解析— IOC 之 开启 bean 的加载

概述 前面我们已经分析了spring对于xml配置文件的解析,将分析的信息组装成 BeanDefinition,并将其保存注册到相应的 BeanDefinitionRegistry 中.至此,Spring IOC 的初始化工作完成.接下来我们将对bean的加载进行探索. 之前系列文章: spring源码深度解析— IOC 之 容器的基本实现 spring源码深度解析— IOC 之 默认标签解析(上) spring源码深度解析— IOC 之 默认标签解析(下) spring源码深度解析— IOC

Spring 源码分析(六)--bean的加载整体分析

通过前面的分析,我们结束了对XML配置文件的解析,接下来将进行bean加载的分析.对于加载bean的功能,在Spring中的调用方式为: 或者   MyTestBean  tb = bf.getBean("myTestBean", MyTestBean.class); 一:整体分析 (1.1)AbstractBeanFactory 类 public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport

spring源码阅读之Bean的加载(二)

在正式分析源码之前,先来了解一下SpringBeans里面最核心的两个类  DefaultListableBeanFactory XMLBean继承自 DefaultListableBeanFactory,而 DefaultListableBeanFactory是整个Bean加载的核心部分,是Sprin注册及加载Bean的默认实现,而对于XmlBeanFactory与 DefaultListableBeanFactory不同的地方其实就是在XmlBeanFactory中使用了自定义的XML读取器

【Spring】详解Spring中Bean的加载

之前写过bean的解析,这篇来讲讲bean的加载,加载要比bean的解析复杂些,该文之前在小编原文中有发表过,要看原文的可以直接点击原文查看,从之前的例子开始,Spring中加载一个bean的方式: TestBean bean = factory.getBean("testBean"); 来看看getBean(String name)方法源码, @Override public Object getBean(String name) throws BeansException { re

spring加载bean的步骤

转载:https://www.cnblogs.com/hljmly/articles/5442778.html spring加载bean的方法 用spring的时候,需要一个applicationContext.xml文件,放到工程目录src下,每次我想获得这个xml文件里的bean的时候,就要这样写: BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml"); 因为是放

spring源码-bean之加载-2

一.前面说了bean的容器初始化,后面当然是说bean的加载.这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉通过BeanFactory是怎么实现class的实例化的. 二.声明一下:XmlBeanFactory和ApplicationContext的不同在与,ApplicationContext在容器初始化的时候,就做了很多准备操作.而XmlBeanFactory是在获取bean的时候才会调用初始化的东西