FactoryBeanRegistrySupport抽象类继承了DefaultSingletonBeanRegistry类,增长了对FactoryBean的处理。segmentfault
// 缓存factoryBean的对应关系 private final Map<String, Object> factoryBeanObjectCache = new ConcurrentHashMap<>(16);
调用factoryBean的getObjectType方法返回class类型缓存
protected Class<?> getTypeForFactoryBean(final FactoryBean<?> factoryBean) { try { if (System.getSecurityManager() != null) { return AccessController.doPrivileged((PrivilegedAction<Class<?>>) factoryBean::getObjectType, getAccessControlContext()); } else { return factoryBean.getObjectType();//调用factoryBean的getObjectType方法返回class类型。 } } catch (Throwable ex) { // Thrown from the FactoryBean's getObjectType implementation. logger.info("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获取FactoryBean安全
protected Object getCachedObjectForFactoryBean(String beanName) { return this.factoryBeanObjectCache.get(beanName); }
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) { //若是是单例,且已实例化 if (factory.isSingleton() && containsSingleton(beanName)) { //对singletonObjects加锁 synchronized (getSingletonMutex()) { Object object = this.factoryBeanObjectCache.get(beanName); if (object == null) { //缓存没有 object = doGetObjectFromFactoryBean(factory, beanName); // 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) // 预防调用上面那个方法时,有对factoryBeanObjectCache设置,因此从新取 Object alreadyThere = this.factoryBeanObjectCache.get(beanName); if (alreadyThere != null) { object = alreadyThere; } else { if (shouldPostProcess) {//对该对象进行后置处理 // 当前正在建立的bean if (isSingletonCurrentlyInCreation(beanName)) { // Temporarily return non-post-processed object, not storing it yet.. 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; } }
调用factoryBean的getObjectType方法返回对象post
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();//调用factoryBean的getObjectType方法返回对象 } } 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) { if (isSingletonCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException( beanName, "FactoryBean which is currently in creation returned null from getObject"); } //没获取到对象,返回NullBean object = new NullBean(); } return object; }
预留钩子this
protected Object postProcessObjectFromFactoryBean(Object object, String beanName) throws BeansException { return object; }
返回FactoryBeanspa
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的对应beanName几个缓存,还要移除factoryBeanObjectCache的缓存code
protected void removeSingleton(String beanName) { synchronized (getSingletonMutex()) { super.removeSingleton(beanName); this.factoryBeanObjectCache.remove(beanName); } }
除了移除DefaultSingletonBeanRegistry的几个缓存,还要移除factoryBeanObjectCache的缓存对象
protected void clearSingletonCache() { synchronized (getSingletonMutex()) { super.clearSingletonCache(); this.factoryBeanObjectCache.clear(); } }
获取安全做用域blog
protected AccessControlContext getAccessControlContext() { return AccessController.getContext(); }