publicObject getBean(String name, Class requiredType, Object[] args)throwsBeansException {
String beanName=transformedBeanName(name);
Object bean=null;//Eagerly check singleton cache for manually registered singletons.Object sharedInstance=null;//从单例缓存中获取synchronized(this.singletonCache) {
sharedInstance=this.singletonCache.get(beanName);
}if(sharedInstance!=null) {if(isSingletonCurrentlyInCreation(beanName)) {if(logger.isDebugEnabled()) {
logger.debug("Returning eagerly cached instance of singleton bean '"+beanName+"' that is not fully initialized yet - a consequence of a circular reference");
}
}else{if(logger.isDebugEnabled()) {
logger.debug("Returning cached instance of singleton bean '"+beanName+"'");
}
}
bean=getObjectForSharedInstance(name, sharedInstance);
}else{//Fail if we're already creating this singleton instance://We're assumably within a circular reference.if(isSingletonCurrentlyInCreation(beanName)) {thrownewBeanCurrentlyInCreationException(beanName);
}//Check if bean definition exists in this factory.//检测bean是否定义在父工厂if(getParentBeanFactory()!=null&&!containsBeanDefinition(beanName)) {//Not found -> check parent.if(getParentBeanFactory()instanceofAbstractBeanFactory) {//Delegation to parent with args only possible for AbstractBeanFactory.return((AbstractBeanFactory) getParentBeanFactory()).getBean(name, requiredType, args);
}elseif(args==null) {//No args -> delegate to standard getBean method.returngetParentBeanFactory().getBean(name, requiredType);
}else{thrownewNoSuchBeanDefinitionException(beanName,"Cannot delegate to parent BeanFactory because it does not supported passed-in arguments");
}
}//获取BeanDefinitionRootBeanDefinition mergedBeanDefinition=getMergedBeanDefinition(beanName,false);
checkMergedBeanDefinition(mergedBeanDefinition, beanName, requiredType, args);//Create bean instance.//创建bean,如果为为singletonif(mergedBeanDefinition.isSingleton()) {synchronized(this.singletonCache) {//Re-check singleton cache within synchronized block.sharedInstance=this.singletonCache.get(beanName);if(sharedInstance==null) {if(logger.isDebugEnabled()) {
logger.debug("Creating shared instance of singleton bean '"+beanName+"'");
}this.currentlyInCreation.add(beanName);try{
sharedInstance=createBean(beanName, mergedBeanDefinition, args);
//加进单例缓存
addSingleton(beanName, sharedInstance);
}catch(BeansException ex) {//Explicitly remove instance from singleton cache: It might have been put there//eagerly by the creation process, to allow for circular reference resolution.//Also remove any beans that received a temporary reference to the bean.destroyDisposableBean(beanName);throwex;
}finally{this.currentlyInCreation.remove(beanName);
}
}
}
bean=getObjectForSharedInstance(name, sharedInstance);
}//如果是prototypeelse{//It's a prototype -> create a new instance.bean=createBean(beanName, mergedBeanDefinition, args);
}
}//Check if required type matches the type of the actual bean instance.if(requiredType!=null&&!requiredType.isAssignableFrom(bean.getClass())) {thrownewBeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}returnbean;
}