通过看spring IOC模块的源码, 可以了解到容器获取bean的核心方法是实现了BeanFactory接口的一个抽象AbstractBeanFactory中 ,核心方法是doGetBean
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 publicT getBean(String name, Class 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 doGetBean( 27 final String name, final Class 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
通过分析代码 大致流程如下:
1.获取到beanName->
2.首先从单态的Map里查找 如果查找到则直接使用说明之前已经获取过 ,如果没找到则
后面有时间补上