通过看spring IOC模块的源码, 可以了解到容器获取bean的核心方法是实现了BeanFactory接口的一个抽象AbstractBeanFactory中 ,核心方法是doGetBean
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 /** 2 * Return an instance, which may be shared or independent, of the specified bean. 3 * @param name the name of the bean to retrieve 4 * @param requiredType the required type of the bean to retrieve 5 * @param args arguments to use when creating a bean instance using explicit arguments 6 * (only applied when creating a new instance as opposed to retrieving an existing one) 7 * @return an instance of the bean 8 * @throws BeansException if the bean could not be created 9 */ 10 public <T> T getBean(String name, Class<T> requiredType, Object... args) throws BeansException { 11 return doGetBean(name, requiredType, args, false); 12 } 13 14 /** 15 * Return an instance, which may be shared or independent, of the specified bean. 16 * @param name the name of the bean to retrieve 17 * @param requiredType the required type of the bean to retrieve 18 * @param args arguments to use when creating a bean instance using explicit arguments 19 * (only applied when creating a new instance as opposed to retrieving an existing one) 20 * @param typeCheckOnly whether the instance is obtained for a type check, 21 * not for actual use 22 * @return an instance of the bean 23 * @throws BeansException if the bean could not be created 24 */ 25 @SuppressWarnings("unchecked") 26 protected <T> T doGetBean( 27 final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) 28 throws BeansException { 29 30 final String beanName = transformedBeanName(name); 31 Object bean; 32 33 // Eagerly check singleton cache for manually registered singletons. 34 Object sharedInstance = getSingleton(beanName); 35 if (sharedInstance != null && args == null) { 36 if (logger.isDebugEnabled()) { 37 if (isSingletonCurrentlyInCreation(beanName)) { 38 logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + 39 "' that is not fully initialized yet - a consequence of a circular reference"); 40 } 41 else { 42 logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); 43 } 44 } 45 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); 46 } 47 48 else { 49 // Fail if we're already creating this bean instance: 50 // We're assumably within a circular reference. 51 if (isPrototypeCurrentlyInCreation(beanName)) { 52 throw new BeanCurrentlyInCreationException(beanName); 53 } 54 55 // Check if bean definition exists in this factory. 56 BeanFactory parentBeanFactory = getParentBeanFactory(); 57 if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { 58 // Not found -> check parent. 59 String nameToLookup = originalBeanName(name); 60 if (args != null) { 61 // Delegation to parent with explicit args. 62 return (T) parentBeanFactory.getBean(nameToLookup, args); 63 } 64 else { 65 // No args -> delegate to standard getBean method. 66 return parentBeanFactory.getBean(nameToLookup, requiredType); 67 } 68 } 69 70 if (!typeCheckOnly) { 71 markBeanAsCreated(beanName); 72 } 73 74 try { 75 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); 76 checkMergedBeanDefinition(mbd, beanName, args); 77 78 // Guarantee initialization of beans that the current bean depends on. 79 String[] dependsOn = mbd.getDependsOn(); 80 if (dependsOn != null) { 81 for (String dep : dependsOn) { 82 if (isDependent(beanName, dep)) { 83 throw new BeanCreationException(mbd.getResourceDescription(), beanName, 84 "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); 85 } 86 registerDependentBean(dep, beanName); 87 getBean(dep); 88 } 89 } 90 91 // Create bean instance. 92 if (mbd.isSingleton()) { 93 sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { 94 @Override 95 public Object getObject() throws BeansException { 96 try { 97 return createBean(beanName, mbd, args); 98 } 99 catch (BeansException ex) { 100 // Explicitly remove instance from singleton cache: It might have been put there 101 // eagerly by the creation process, to allow for circular reference resolution. 102 // Also remove any beans that received a temporary reference to the bean. 103 destroySingleton(beanName); 104 throw ex; 105 } 106 } 107 }); 108 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); 109 } 110 111 else if (mbd.isPrototype()) { 112 // It's a prototype -> create a new instance. 113 Object prototypeInstance = null; 114 try { 115 beforePrototypeCreation(beanName); 116 prototypeInstance = createBean(beanName, mbd, args); 117 } 118 finally { 119 afterPrototypeCreation(beanName); 120 } 121 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); 122 } 123 124 else { 125 String scopeName = mbd.getScope(); 126 final Scope scope = this.scopes.get(scopeName); 127 if (scope == null) { 128 throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); 129 } 130 try { 131 Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() { 132 @Override 133 public Object getObject() throws BeansException { 134 beforePrototypeCreation(beanName); 135 try { 136 return createBean(beanName, mbd, args); 137 } 138 finally { 139 afterPrototypeCreation(beanName); 140 } 141 } 142 }); 143 bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); 144 } 145 catch (IllegalStateException ex) { 146 throw new BeanCreationException(beanName, 147 "Scope '" + scopeName + "' is not active for the current thread; consider " + 148 "defining a scoped proxy for this bean if you intend to refer to it from a singleton", 149 ex); 150 } 151 } 152 } 153 catch (BeansException ex) { 154 cleanupAfterBeanCreationFailure(beanName); 155 throw ex; 156 } 157 } 158 159 // Check if required type matches the type of the actual bean instance. 160 if (requiredType != null && bean != null && !requiredType.isInstance(bean)) { 161 try { 162 return getTypeConverter().convertIfNecessary(bean, requiredType); 163 } 164 catch (TypeMismatchException ex) { 165 if (logger.isDebugEnabled()) { 166 logger.debug("Failed to convert bean '" + name + "' to required type '" + 167 ClassUtils.getQualifiedName(requiredType) + "'", ex); 168 } 169 throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); 170 } 171 } 172 return (T) bean; 173 }
通过分析代码 大致流程如下:
1.获取到beanName->
2.首先从单态的Map里查找 如果查找到则直接使用说明之前已经获取过 ,如果没找到则
后面有时间补上