parentBeanFactory和依赖处理
如果从单例缓存中没有获取到单例 Bean 对象,则说明两种两种情况:
该 Bean 的 scope 不是 singleton
该 Bean 的 scope 是 singleton ,但是没有初始化完成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException (beanName); }BeanFactory parentBeanFactory = getParentBeanFactory();if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { String nameToLookup = originalBeanName(name); if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null ) { return (T) parentBeanFactory.getBean(nameToLookup, args); } else if (requiredType != null ) { return parentBeanFactory.getBean(nameToLookup, requiredType); } else { return (T) parentBeanFactory.getBean(nameToLookup); } }if (!typeCheckOnly) { markBeanAsCreated(beanName); }try { final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null ) { for (String dep : dependsOn) { if (isDependent(beanName, dep)) { throw new BeanCreationException (mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'" ); } registerDependentBean(dep, beanName); try { getBean(dep); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException (mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'" , ex); } } } }
该段主要处理了:
1、检测,若当前 Bean 在创建,则抛出 BeanCurrentlyInCreationException 异常。
2、如果 beanDefinitionMap 中不存在 beanName 的 BeanDefinition(即在 Spring bean 初始化过程中没有加载),则尝试从 parentBeanFactory 中加载。
3、判断是否为类型检查。
4、从 mergedBeanDefinitions
中获取 beanName
对应的 RootBeanDefinition 对象。如果这个 BeanDefinition 是子 Bean 的话,则会合并父类的相关属性。
5、依赖处理。
检测 Spring 只解决单例模式下的循环依赖,对于原型模式的循环依赖则是抛出 BeanCurrentlyInCreationException 异常,所以首先检查该 beanName
是否处于原型模式下的循环依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 private final ThreadLocal<Object> prototypesCurrentlyInCreation = new NamedThreadLocal <>("Prototype beans currently in creation" );if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException (beanName); }protected boolean isPrototypeCurrentlyInCreation (String beanName) { Object curVal = this .prototypesCurrentlyInCreation.get(); return (curVal != null && (curVal.equals(beanName) || (curVal instanceof Set && ((Set<?>) curVal).contains(beanName)))); }
检查父类BeanFactory
若 containsBeanDefinition(String beanName)
方法中不存在 beanName
相对应的 BeanDefinition 对象时,则从 parentBeanFactory
中获取。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 BeanFactory parentBeanFactory = getParentBeanFactory();if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { String nameToLookup = originalBeanName(name); if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null ) { return (T) parentBeanFactory.getBean(nameToLookup, args); } else { return parentBeanFactory.getBean(nameToLookup, requiredType); } }
在获取实例Bean之前,先通过originalBeanName()
方法对name
进行了处理用来获取原始BeanName。
1 2 3 4 5 6 7 8 protected String originalBeanName (String name) { String beanName = transformedBeanName(name); if (name.startsWith(FACTORY_BEAN_PREFIX)) { beanName = FACTORY_BEAN_PREFIX + beanName; } return beanName; }
优先调用了transformedBeanName()
方法(doGetBean()
方法第一步就是调用了该方法),然后再对返回的beanname进行处理,因为上一步将&
去掉了。
类型检查 方法参数 typeCheckOnly
,是用来判断调用 getBean(...)
方法时,表示是否为仅仅 进行类型检查获取 Bean 对象。如果不是仅仅做类型检查,而是创建 Bean 对象,则需要调用 markBeanAsCreated(String beanName)
方法,进行记录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Override public Object getBean (String name) throws BeansException { return doGetBean(name, null , null , false ); }protected <T> T doGetBean (String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException { if (!typeCheckOnly) { markBeanAsCreated(beanName); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 protected void markBeanAsCreated (String beanName) { if (!this .alreadyCreated.contains(beanName)) { synchronized (this .mergedBeanDefinitions) { if (!this .alreadyCreated.contains(beanName)) { clearMergedBeanDefinition(beanName); this .alreadyCreated.add(beanName); } } } }protected void clearMergedBeanDefinition (String beanName) { RootBeanDefinition bd = this .mergedBeanDefinitions.get(beanName); if (bd != null ) { bd.stale = true ; } }
获取RootBeanDefinition
1 2 3 4 5 6 RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args);
调用 getMergedLocalBeanDefinition(String beanName)
方法,获取相对应的 BeanDefinition 对象。
1 2 3 4 5 6 7 8 9 10 11 protected RootBeanDefinition getMergedLocalBeanDefinition (String beanName) throws BeansException { RootBeanDefinition mbd = this .mergedBeanDefinitions.get(beanName); if (mbd != null && !mbd.stale) { return mbd; } return getMergedBeanDefinition(beanName, getBeanDefinition(beanName)); }
调用 checkMergedBeanDefinition()
方法,检查给定的合并的 BeanDefinition 对象。
1 2 3 4 5 6 7 protected void checkMergedBeanDefinition (RootBeanDefinition mbd, String beanName, @Nullable Object[] args) throws BeanDefinitionStoreException { if (mbd.isAbstract()) { throw new BeanIsAbstractException (beanName); } }
处理依赖 如果一个 Bean 有依赖 Bean 的话,那么在初始化该 Bean 时是需要先初始化它所依赖的 Bean 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 String[] dependsOn = mbd.getDependsOn();if (dependsOn != null ) { for (String dep : dependsOn) { if (isDependent(beanName, dep)) { throw new BeanCreationException (mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'" ); } registerDependentBean(dep, beanName); try { getBean(dep); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException (mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'" , ex); } } }
通过迭代的方式依次对依赖 bean 进行检测、校验。如果通过,则调用 getBean(String beanName)
方法,实例化依赖 的 Bean 对象。
getDependsOn()
获取所有依赖,RootBeanDefinition
继承自AbstractBeanDefinition
所以委托给了父类处理:
1 2 3 4 5 6 7 @Nullable private String[] dependsOn;public String[] getDependsOn() { return this .dependsOn; }
isDependent()
校验该依赖是否已经注册给当前 Bean 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 protected boolean isDependent (String beanName, String dependentBeanName) { synchronized (this .dependentBeanMap) { return isDependent(beanName, dependentBeanName, null ); } }private boolean isDependent (String beanName, String dependentBeanName, @Nullable Set<String> alreadySeen) { if (alreadySeen != null && alreadySeen.contains(beanName)) { return false ; } String canonicalName = canonicalName(beanName); Set<String> dependentBeans = this .dependentBeanMap.get(canonicalName); if (dependentBeans == null ) { return false ; } if (dependentBeans.contains(dependentBeanName)) { return true ; } for (String transitiveDependency : dependentBeans) { if (alreadySeen == null ) { alreadySeen = new HashSet <>(); } alreadySeen.add(beanName); if (isDependent(transitiveDependency, dependentBeanName, alreadySeen)) { return true ; } } return false ; }
registerDependentBean(dep, beanName)方法;
注册依赖记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public void registerDependentBean (String beanName, String dependentBeanName) { String canonicalName = canonicalName(beanName); synchronized (this .dependentBeanMap) { Set<String> dependentBeans = this .dependentBeanMap.computeIfAbsent(canonicalName, k -> new LinkedHashSet <>(8 )); if (!dependentBeans.add(dependentBeanName)) { return ; } } synchronized (this .dependenciesForBeanMap) { Set<String> dependenciesForBean = this .dependenciesForBeanMap.computeIfAbsent(dependentBeanName, k -> new LinkedHashSet <>(8 )); dependenciesForBean.add(canonicalName); } }
getBean()
方法最后调用 getBean(String beanName)
方法,实例化依赖 Bean 对象。