IOC doGetBean 方法解析(二)

方法分析(3)spring

org.springframework.beans.factory.support.AbstractBeanFactory.markBeanAsCreated(String)緩存

protected void markBeanAsCreated(String beanName) { if (!this.alreadyCreated.contains(beanName)) {//private final Set<String> alreadyCreated = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(256));
synchronized (this.mergedBeanDefinitions) {
//這裏在同步塊內再作一次判斷,雙重判斷
if (!this.alreadyCreated.contains(beanName)) { //初始化的時候第一次進入確定是沒有 clearMergedBeanDefinition(beanName);//代碼(1) this.alreadyCreated.add(beanName);//而後在 alreadyCreated 這個 ConcurrentHashMap 中放入這個 beanName } } } }

//代碼(1)
//private final Map<String, RootBeanDefinition> mergedBeanDefinitions = new ConcurrentHashMap<String, RootBeanDefinition>(256);
protected void clearMergedBeanDefinition(String beanName) { this.mergedBeanDefinitions.remove(beanName); }

 

方法分析(4)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(String)
private final Map<String, RootBeanDefinition> mergedBeanDefinitions = new ConcurrentHashMap<String, RootBeanDefinition>(256);
protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException { //初始化第一次建立bean mergedBeanDefinitions 中不包含該bean 
        RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName); if (mbd != null) { return mbd; } return getMergedBeanDefinition(beanName, getBeanDefinition(beanName)); }

一、-----org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(String)ide

public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException { BeanDefinition bd = this.beanDefinitionMap.get(beanName);//在初始化時 obtainFreshBeanFactory 方法被賦值 if (bd == null) { if (this.logger.isTraceEnabled()) { this.logger.trace("No bean named '" + beanName + "' found in " + this); } throw new NoSuchBeanDefinitionException(beanName); } return bd; }
二、---org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(String, BeanDefinition)
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd) throws BeanDefinitionStoreException { return getMergedBeanDefinition(beanName, bd, null); }

三、--org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(String, BeanDefinition, BeanDefinition)this

protected RootBeanDefinition getMergedBeanDefinition( String beanName, BeanDefinition bd, BeanDefinition containingBd) throws BeanDefinitionStoreException { synchronized (this.mergedBeanDefinitions) { RootBeanDefinition mbd = null; // Check with full lock now in order to enforce the same merged instance.
            if (containingBd == null) { mbd = this.mergedBeanDefinitions.get(beanName); } if (mbd == null) { if (bd.getParentName() == null) { // Use copy of given root bean definition.
                    if (bd instanceof RootBeanDefinition) { mbd = ((RootBeanDefinition) bd).cloneBeanDefinition(); } else {  mbd = new RootBeanDefinition(bd); } } else { // Child bean definition: needs to be merged with parent.
 BeanDefinition pbd; try { String parentBeanName = transformedBeanName(bd.getParentName()); if (!beanName.equals(parentBeanName)) { pbd = getMergedBeanDefinition(parentBeanName); } else { BeanFactory parent = getParentBeanFactory(); if (parent instanceof ConfigurableBeanFactory) { pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName); } else { throw new NoSuchBeanDefinitionException(parentBeanName, "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName +
                                        "': cannot be resolved without an AbstractBeanFactory parent"); } } } catch (NoSuchBeanDefinitionException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Could not resolve parent bean definition '" + bd.getParentName() + "'", ex); } // Deep copy with overridden values.
                    mbd = new RootBeanDefinition(pbd); mbd.overrideFrom(bd); } // 若是爲空則設爲單例
                if (!StringUtils.hasLength(mbd.getScope())) { mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON); } //這裏意思是一個不是單例的bean,其中也不能包含單例 bean,在這裏檢查並修正
                if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) { mbd.setScope(containingBd.getScope()); } //當接下來要建立這個 bean ,或者已經建立過一次了,beanDefinition 就放入緩存中
                if (containingBd == null && isCacheBeanMetadata()) { this.mergedBeanDefinitions.put(beanName, mbd);//放入緩存 } } return mbd; } }
相關文章
相關標籤/搜索