spring IOC之篇六 bean的加载---bean的创建
Posted 烟尘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
以上是关于spring IOC之篇六 bean的加载---bean的创建的主要内容,如果未能解决你的问题,请参考以下文章
[死磕 Spring 16/43] --- IOC 之开启 bean 的加载
[死磕 Spring 3/43] --- IOC 之 加载 Bean