从FactoryBeanRegistrySupport类的名字能够看出FactoryBeanRegistrySupport负责FactoryBean的注册与支持。若是想知道FactoryBean相关的资料,请阅读spring-bean中关于FactoryBean的解读。java
public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanRegistry { // 缓存singleton性质的FactoryBean从getObject获得的对象 private final Map<String, Object> factoryBeanObjectCache = new ConcurrentHashMap<String, Object>(16); // 从factoryBean.getObjectType()中获得obejctTpye。 protected Class<?> getTypeForFactoryBean(final FactoryBean<?> factoryBean) { try { if (System.getSecurityManager() != null) { return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() { @Override public Class<?> run() { return factoryBean.getObjectType(); } }, getAccessControlContext()); } else { return factoryBean.getObjectType(); } } catch (Throwable ex) { // Thrown from the FactoryBean's getObjectType implementation. logger.warn("FactoryBean threw exception from getObjectType, despite the contract saying " + "that it should return null if the type of its object cannot be determined yet", ex); return null; } } // 经过beanName从缓存中获得对象 protected Object getCachedObjectForFactoryBean(String beanName) { Object object = this.factoryBeanObjectCache.get(beanName); return (object != NULL_OBJECT ? object : null); } // 调用getCachedObjectForFactoryBean以后,识别结果为空,才会调用getObjectFromFactoryBean方法 // protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) { if (factory.isSingleton() && containsSingleton(beanName)) { synchronized (getSingletonMutex()) {// 得到DefaultSingletonBeanRegistry中的锁 Object object = this.factoryBeanObjectCache.get(beanName);// 在并发时候,线程A,B注入A(FactoryBean),A线程注册时,没有, if (object == null) {// A线程注册时,没有, 就从FactoryBean 获得 object object = doGetObjectFromFactoryBean(factory, beanName);// 从FactoryBean 获得 object // Only post-process and store if not put there already during getObject() call above // (e.g. because of circular reference processing triggered by custom getBean calls) Object alreadyThere = this.factoryBeanObjectCache.get(beanName);// 为何在次操做,上面英语给出了答案 if (alreadyThere != null) { object = alreadyThere; } else { if (object != null && shouldPostProcess) { try { object = postProcessObjectFromFactoryBean(object, beanName);// 处理从FactoryBean获得的对象 } catch (Throwable ex) { throw new BeanCreationException(beanName, "Post-processing of FactoryBean's singleton object failed", ex); } } this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));//保存在缓存中 } } return (object != NULL_OBJECT ? object : null); } } else { Object object = doGetObjectFromFactoryBean(factory, beanName); if (object != null && shouldPostProcess) { try { object = postProcessObjectFromFactoryBean(object, beanName); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex); } } return object; } } // 执行调用FactoryBean.getObject()方法而已 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(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { return 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); } // Do not accept a null value for a FactoryBean that's not fully // initialized yet: Many FactoryBeans just return null then. if (object == null && isSingletonCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException( beanName, "FactoryBean which is currently in creation returned null from getObject"); } return object; } protected Object postProcessObjectFromFactoryBean(Object object, String beanName) throws BeansException { return object; } protected FactoryBean<?> getFactoryBean(String beanName, Object beanInstance) throws BeansException { if (!(beanInstance instanceof FactoryBean)) { throw new BeanCreationException(beanName, "Bean instance of type [" + beanInstance.getClass() + "] is not a FactoryBean"); } return (FactoryBean<?>) beanInstance; } // 重写了DefaultSingletonBeanRegistry的removeSingleton 方法 protected void removeSingleton(String beanName) { super.removeSingleton(beanName); this.factoryBeanObjectCache.remove(beanName); } protected AccessControlContext getAccessControlContext() { return AccessController.getContext(); } }
FactoryBeanRegistrySupport整体仍是至关简单。可是仍是有不少细节spring