引言 从单例缓存中获取的bean原始实例,通过单例模式、原型模式或者request/session/global session等获取的bean原始实例,可能并不是我们想要的bean对象,需要通过方法:getObjectForBeanInstance获取真正的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 37 38 39 40 41 42 43 44 45 46 protected Object getObjectForBeanInstance ( Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) { if (BeanFactoryUtils.isFactoryDereference(name)) { if (beanInstance instanceof NullBean) { return beanInstance; } if (!(beanInstance instanceof FactoryBean)) { throw new BeanIsNotAFactoryException (beanName, beanInstance.getClass()); } } if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) { return beanInstance; } Object object = null ; if (mbd == null ) { object = getCachedObjectForFactoryBean(beanName); } if (object == null ) { FactoryBean<?> factory = (FactoryBean<?>) beanInstance; if (mbd == null && containsBeanDefinition(beanName)) { mbd = getMergedLocalBeanDefinition(beanName); } boolean synthetic = (mbd != null && mbd.isSynthetic()); object = getObjectFromFactoryBean(factory, beanName, !synthetic); } return object; }
首先各种判断校验,返回beanInstance或者抛出异常,之后从缓存中尝试获取object。
1 2 3 4 5 6 7 private final Map<String, Object> factoryBeanObjectCache = new ConcurrentHashMap <>(16 ); @Nullable protected Object getCachedObjectForFactoryBean (String beanName) { return this .factoryBeanObjectCache.get(beanName); }
如果缓存中没有,则beanInstance一定是FactoryBean。在这个if分支中,先组装RootBeanDefinition,再通过getObjectFromFactoryBean方法获取对象。
1、getMergedLocalBeanDefinition 返回合并的RootBeanDefinition,如果指定的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 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 protected RootBeanDefinition getMergedLocalBeanDefinition (String beanName) throws BeansException { RootBeanDefinition mbd = this .mergedBeanDefinitions.get(beanName); if (mbd != null ) { return mbd; } return getMergedBeanDefinition(beanName, getBeanDefinition(beanName)); } protected RootBeanDefinition getMergedBeanDefinition (String beanName, BeanDefinition bd) throws BeanDefinitionStoreException { return getMergedBeanDefinition(beanName, bd, null ); } protected RootBeanDefinition getMergedBeanDefinition ( String beanName, BeanDefinition bd, @Nullable BeanDefinition containingBd) throws BeanDefinitionStoreException { synchronized (this .mergedBeanDefinitions) { RootBeanDefinition mbd = null ; if (containingBd == null ) { mbd = this .mergedBeanDefinitions.get(beanName); } if (mbd == null ) { if (bd.getParentName() == null ) { if (bd instanceof RootBeanDefinition) { mbd = ((RootBeanDefinition) bd).cloneBeanDefinition(); } else { mbd = new RootBeanDefinition (bd); } } else { BeanDefinition pbd; try { String parentBeanName = transformedBeanName(bd.getParentName()); if (!beanName.equals(parentBeanName)) { pbd = getMergedBeanDefinition(parentBeanName); } else { BeanFactory parent = getParentBeanFactory(); if (parent instanceof ConfigurableBeanFactory) { pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName); } else { throw new NoSuchBeanDefinitionException (parentBeanName, "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName + "': cannot be resolved without an AbstractBeanFactory parent" ); } } } catch (NoSuchBeanDefinitionException ex) { throw new BeanDefinitionStoreException (bd.getResourceDescription(), beanName, "Could not resolve parent bean definition '" + bd.getParentName() + "'" , ex); } mbd = new RootBeanDefinition (pbd); mbd.overrideFrom(bd); } if (!StringUtils.hasLength(mbd.getScope())) { mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON); } if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) { mbd.setScope(containingBd.getScope()); } if (containingBd == null && isCacheBeanMetadata()) { this .mergedBeanDefinitions.put(beanName, mbd); } } return mbd; } }
代码比较长,主要就是整合父类的各种属性,最后返回RootBeanDifinition。
2、getObjectFromFactoryBean 获取要从给定FactoryBean公开的对象。
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 64 65 66 67 68 protected Object getObjectFromFactoryBean (FactoryBean<?> factory, String beanName, boolean shouldPostProcess) { if (factory.isSingleton() && containsSingleton(beanName)) { synchronized (getSingletonMutex()) { Object object = this .factoryBeanObjectCache.get(beanName); if (object == null ) { object = doGetObjectFromFactoryBean(factory, beanName); Object alreadyThere = this .factoryBeanObjectCache.get(beanName); if (alreadyThere != null ) { object = alreadyThere; } else { if (shouldPostProcess) { if (isSingletonCurrentlyInCreation(beanName)) { return object; } beforeSingletonCreation(beanName); try { object = postProcessObjectFromFactoryBean(object, beanName); } catch (Throwable ex) { throw new BeanCreationException (beanName, "Post-processing of FactoryBean's singleton object failed" , ex); } finally { afterSingletonCreation(beanName); } } if (containsSingleton(beanName)) { this .factoryBeanObjectCache.put(beanName, object); } } } return object; } } else { Object object = doGetObjectFromFactoryBean(factory, beanName); if (shouldPostProcess) { try { object = postProcessObjectFromFactoryBean(object, beanName); } catch (Throwable ex) { throw new BeanCreationException (beanName, "Post-processing of FactoryBean's object failed" , ex); } } return object; } }
比较重要的方法:
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 private Object doGetObjectFromFactoryBean (final FactoryBean<?> factory, final String beanName) throws BeanCreationException { Object object; try { if (System.getSecurityManager() != null ) { AccessControlContext acc = getAccessControlContext(); try { object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { object = factory.getObject(); } } catch (FactoryBeanNotInitializedException ex) { throw new BeanCurrentlyInCreationException (beanName, ex.toString()); } catch (Throwable ex) { throw new BeanCreationException (beanName, "FactoryBean threw exception on object creation" , ex); } if (object == null ) { if (isSingletonCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException ( beanName, "FactoryBean which is currently in creation returned null from getObject" ); } object = new NullBean (); } return object; }
最后通过factory.getObject()方法获取对象。