Spring 소스 분석 - IOC 의 Default SingletonBeanRegistry

16897 단어 spring
전편 에 서 는 주로 인터페이스 SingletonBean Registry 를 다 루 었 습 니 다. 이 편 에 서 는 Default SingletonBean Registry, Default SingletonBean Registry 가 인터페이스 SingletonBean Registry 의 각 함수 에 대한 실현 을 다 루 었 습 니 다. 구체 적 인 코드 는 다음 과 같 습 니 다.
/**DefaultSingletonBeanRegistry    SingletonBeanRegistry ,          ,            SimpleAliasRegistry ,         
 */
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {

	//         ,  maps   null
	protected static final Object NULL_OBJECT = new Object();
	//      
	protected final Log logger = LogFactory.getLog(getClass());

	//    singleton     
	private final Map singletonObjects = new ConcurrentHashMap(64);

	//      singleton        
	private final Map> singletonFactories = new HashMap>(16);

	//       singleton     
	private final Map earlySingletonObjects = new HashMap(16);

	//           ,      
	private final Set registeredSingletons = new LinkedHashSet(64);

	//            
	private final Set singletonsCurrentlyInCreation =
			Collections.newSetFromMap(new ConcurrentHashMap(16));

	//             bean name  
	private final Set inCreationCheckExclusions =
			Collections.newSetFromMap(new ConcurrentHashMap(16));

	//      
	private Set suppressedExceptions;

	//     singleton   
	private boolean singletonsCurrentlyInDestruction = false;

	//            
	private final Map disposableBeans = new LinkedHashMap();

	//  bean       bean     bean           
	private final Map> containedBeanMap = new ConcurrentHashMap>(16);

	//  bean     bean   bean          
	private final Map> dependentBeanMap = new ConcurrentHashMap>(64);

	//  bean     bean        bean        
	private final Map> dependenciesForBeanMap = new ConcurrentHashMap>(64);

    //      
	@Override
	public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
        //beanName    
		Assert.notNull(beanName, "'beanName' must not be null");
        //        
		synchronized (this.singletonObjects) {
            //            ,          
			Object oldObject = this.singletonObjects.get(beanName);
			if (oldObject != null) {
				throw new IllegalStateException("Could not register object [" + singletonObject +
						"] under bean name '" + beanName + "': there is already object [" + oldObject + "] bound");
			}
            //          
			addSingleton(beanName, singletonObject);
		}
	}

	//           
	protected void addSingleton(String beanName, Object singletonObject) {
        //           
		synchronized (this.singletonObjects) {
             //                  null  ConcurrentHashMap   null
			this.singletonObjects.put(beanName, (singletonObject != null ? singletonObject : NULL_OBJECT));
             // beanName       singletonObjects  ,  singletonFactories         beanName    
			this.singletonFactories.remove(beanName);
			   //     earlySingletonObjects         beanName bean    
            this.earlySingletonObjects.remove(beanName);
            //            beanName
			this.registeredSingletons.add(beanName);
		}
	}

	//  beanName   factory
	protected void addSingletonFactory(String beanName, ObjectFactory> singletonFactory) {
		Assert.notNull(singletonFactory, "Singleton factory must not be null");
		synchronized (this.singletonObjects) {
			if (!this.singletonObjects.containsKey(beanName)) {
				this.singletonFactories.put(beanName, singletonFactory);
				this.earlySingletonObjects.remove(beanName);
				this.registeredSingletons.add(beanName);
			}
		}
	}
    //      ,           
	@Override
	public Object getSingleton(String beanName) {
		return getSingleton(beanName, true);
	}

	//         
	protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        //            
		Object singletonObject = this.singletonObjects.get(beanName);
        //         
        //               
		if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
            //     ,      
			synchronized (this.singletonObjects) {
                //           
                //       ,early singleton beanFactory      
                //                
				singletonObject = this.earlySingletonObjects.get(beanName);
				if (singletonObject == null && allowEarlyReference) {
                    //            
					ObjectFactory> singletonFactory = this.singletonFactories.get(beanName);
					if (singletonFactory != null) {
                        //                 
						singletonObject = singletonFactory.getObject();
                        //              earlySingleton       
						this.earlySingletonObjects.put(beanName, singletonObject);
                        //          
						this.singletonFactories.remove(beanName);
					}
				}
			}
		}
        //           null
		return (singletonObject != NULL_OBJECT ? singletonObject : null);
	}

	//          
	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) {
				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 + "'");
				}
                //      ,                
				beforeSingletonCreation(beanName);
				boolean newSingleton = false;
				boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
				if (recordSuppressedExceptions) {
					this.suppressedExceptions = new LinkedHashSet();
				}
				try {
                    //        
					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;
					}
					afterSingletonCreation(beanName);
				}
				if (newSingleton) {
					addSingleton(beanName, singletonObject);
				}
			}
			return (singletonObject != NULL_OBJECT ? singletonObject : null);
		}
	}

	//      
	protected void onSuppressedException(Exception ex) {
		synchronized (this.singletonObjects) {
			if (this.suppressedExceptions != null) {
				this.suppressedExceptions.add(ex);
			}
		}
	}

	//     beanName   
	protected void removeSingleton(String beanName) {
		synchronized (this.singletonObjects) {
			this.singletonObjects.remove(beanName);
			this.singletonFactories.remove(beanName);
			this.earlySingletonObjects.remove(beanName);
			this.registeredSingletons.remove(beanName);
		}
	}
    //               
	@Override
	public boolean containsSingleton(String beanName) {
		return this.singletonObjects.containsKey(beanName);
	}

    //           
	@Override
	public String[] getSingletonNames() {
		synchronized (this.singletonObjects) {
			return StringUtils.toStringArray(this.registeredSingletons);
		}
	}

    //         
	@Override
	public int getSingletonCount() {
		synchronized (this.singletonObjects) {
			return this.registeredSingletons.size();
		}
	}


	public void setCurrentlyInCreation(String beanName, boolean inCreation) {
		Assert.notNull(beanName, "Bean name must not be null");
		if (!inCreation) {
			this.inCreationCheckExclusions.add(beanName);
		}
		else {
			this.inCreationCheckExclusions.remove(beanName);
		}
	}

	public boolean isCurrentlyInCreation(String beanName) {
		Assert.notNull(beanName, "Bean name must not be null");
		return (!this.inCreationCheckExclusions.contains(beanName) && isActuallyInCreation(beanName));
	}

    ///            
	protected boolean isActuallyInCreation(String beanName) {
		return isSingletonCurrentlyInCreation(beanName);
	}

	//            
	public boolean isSingletonCurrentlyInCreation(String beanName) {
		return this.singletonsCurrentlyInCreation.contains(beanName);
	}

	//        ,          
	protected void beforeSingletonCreation(String beanName) {
		if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
			throw new BeanCurrentlyInCreationException(beanName);
		}
	}

	//        ,        
	protected void afterSingletonCreation(String beanName) {
		if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
			throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
		}
	}


	//                      
	public void registerDisposableBean(String beanName, DisposableBean bean) {
		synchronized (this.disposableBeans) {
			this.disposableBeans.put(beanName, bean);
		}
	}

	//    bean         
	public void registerContainedBean(String containedBeanName, String containingBeanName) {
		// A quick check for an existing entry upfront, avoiding synchronization...
		Set containedBeans = this.containedBeanMap.get(containingBeanName);
		if (containedBeans != null && containedBeans.contains(containedBeanName)) {
			return;
		}

		// No entry yet -> fully synchronized manipulation of the containedBeans Set
		synchronized (this.containedBeanMap) {
			containedBeans = this.containedBeanMap.get(containingBeanName);
			if (containedBeans == null) {
				containedBeans = new LinkedHashSet(8);
				this.containedBeanMap.put(containingBeanName, containedBeans);
			}
			containedBeans.add(containedBeanName);
		}
		registerDependentBean(containedBeanName, containingBeanName);
	}

	//    bean     bean,   bean       
	public void registerDependentBean(String beanName, String dependentBeanName) {
		// A quick check for an existing entry upfront, avoiding synchronization...
		String canonicalName = canonicalName(beanName);
		Set dependentBeans = this.dependentBeanMap.get(canonicalName);
		if (dependentBeans != null && dependentBeans.contains(dependentBeanName)) {
			return;
		}

		// No entry yet -> fully synchronized manipulation of the dependentBeans Set
		synchronized (this.dependentBeanMap) {
			dependentBeans = this.dependentBeanMap.get(canonicalName);
			if (dependentBeans == null) {
				dependentBeans = new LinkedHashSet(8);
				this.dependentBeanMap.put(canonicalName, dependentBeans);
			}
			dependentBeans.add(dependentBeanName);
		}
		synchronized (this.dependenciesForBeanMap) {
			Set dependenciesForBean = this.dependenciesForBeanMap.get(dependentBeanName);
			if (dependenciesForBean == null) {
				dependenciesForBean = new LinkedHashSet(8);
				this.dependenciesForBeanMap.put(dependentBeanName, dependenciesForBean);
			}
			dependenciesForBean.add(canonicalName);
		}
	}

	//  beanName    dependentBeanName
	protected boolean isDependent(String beanName, String dependentBeanName) {
		String canonicalName = canonicalName(beanName);
		Set dependentBeans = this.dependentBeanMap.get(canonicalName);
		if (dependentBeans == null) {
			return false;
		}
		if (dependentBeans.contains(dependentBeanName)) {
			return true;
		}
		for (String transitiveDependency : dependentBeans) {
			if (isDependent(transitiveDependency, dependentBeanName)) {
				return true;
			}
		}
		return false;
	}

	//  beanName      bean
	protected boolean hasDependentBean(String beanName) {
		return this.dependentBeanMap.containsKey(beanName);
	}

    //        bean   bean   ,     。 
	public String[] getDependentBeans(String beanName) {
		Set dependentBeans = this.dependentBeanMap.get(beanName);
		if (dependentBeans == null) {
			return new String[0];
		}
		return StringUtils.toStringArray(dependentBeans);
	}

	//  beanName     bean
	public String[] getDependenciesForBean(String beanName) {
		Set dependenciesForBean = this.dependenciesForBeanMap.get(beanName);
		if (dependenciesForBean == null) {
			return new String[0];
		}
		return dependenciesForBean.toArray(new String[dependenciesForBean.size()]);
	}
    
    //      
	public void destroySingletons() {
		if (logger.isDebugEnabled()) {
			logger.debug("Destroying singletons in " + this);
		}
		synchronized (this.singletonObjects) {
			this.singletonsCurrentlyInDestruction = true;
		}

		String[] disposableBeanNames;
		synchronized (this.disposableBeans) {
			disposableBeanNames = StringUtils.toStringArray(this.disposableBeans.keySet());
		}
		for (int i = disposableBeanNames.length - 1; i >= 0; i--) {
			destroySingleton(disposableBeanNames[i]);
		}

		this.containedBeanMap.clear();
		this.dependentBeanMap.clear();
		this.dependenciesForBeanMap.clear();

		synchronized (this.singletonObjects) {
			this.singletonObjects.clear();
			this.singletonFactories.clear();
			this.earlySingletonObjects.clear();
			this.registeredSingletons.clear();
			this.singletonsCurrentlyInDestruction = false;
		}
	}

	//    beanName   
	public void destroySingleton(String beanName) {
		// Remove a registered singleton of the given name, if any.
		removeSingleton(beanName);

		// Destroy the corresponding DisposableBean instance.
		DisposableBean disposableBean;
		synchronized (this.disposableBeans) {
			disposableBean = (DisposableBean) this.disposableBeans.remove(beanName);
		}
		destroyBean(beanName, disposableBean);
	}

	//    beanName   
	protected void destroyBean(String beanName, DisposableBean bean) {
		// Trigger destruction of dependent beans first...
		Set dependencies = this.dependentBeanMap.remove(beanName);
		if (dependencies != null) {
			if (logger.isDebugEnabled()) {
				logger.debug("Retrieved dependent beans for bean '" + beanName + "': " + dependencies);
			}
			for (String dependentBeanName : dependencies) {
				destroySingleton(dependentBeanName);
			}
		}

		// Actually destroy the bean now...
		if (bean != null) {
			try {
				bean.destroy();
			}
			catch (Throwable ex) {
				logger.error("Destroy method on bean with name '" + beanName + "' threw an exception", ex);
			}
		}

		// Trigger destruction of contained beans...
		Set containedBeans = this.containedBeanMap.remove(beanName);
		if (containedBeans != null) {
			for (String containedBeanName : containedBeans) {
				destroySingleton(containedBeanName);
			}
		}

		// Remove destroyed bean from other beans' dependencies.
		synchronized (this.dependentBeanMap) {
			for (Iterator>> it = this.dependentBeanMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry> entry = it.next();
				Set dependenciesToClean = entry.getValue();
				dependenciesToClean.remove(beanName);
				if (dependenciesToClean.isEmpty()) {
					it.remove();
				}
			}
		}

		// Remove destroyed bean's prepared dependency information.
		this.dependenciesForBeanMap.remove(beanName);
	}

	/**
	 * Expose the singleton mutex to subclasses.
	 * 

Subclasses should synchronize on the given Object if they perform * any sort of extended singleton creation phase. In particular, subclasses * should not have their own mutexes involved in singleton creation, * to avoid the potential for deadlocks in lazy-init situations. */ protected final Object getSingletonMutex() { return this.singletonObjects; } }


좋은 웹페이지 즐겨찾기