上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry—— Spring源码分析——BeanFactory体系之抽象类、类分析(一),今天继续分析。
一、工厂Bean注册支持——FactoryBeanRegistrySupport
废话不多说,直接看我注释的源码:
/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.beans.factory.support; import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCurrentlyInCreationException; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBeanNotInitializedException; /** * Support base class for singleton registries which need to handle * {@link org.springframework.beans.factory.FactoryBean} instances, * integrated with {@link DefaultSingletonBeanRegistry}'s singleton management. * * <p>Serves as base class for {@link AbstractBeanFactory}. * * @author Juergen Hoeller * @since 2.5.1 */ public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanRegistry { /** 工厂Bean生产的单例的集合: FactoryBean name --> object */ private final Map<String, Object> factoryBeanObjectCache = new ConcurrentHashMap<String, Object>(16); //返回指定FactoryBean的类型 protected Class getTypeForFactoryBean(final FactoryBean factoryBean) { try { if (System.getSecurityManager() != null) {//如果当前系统存在安全管理器 return AccessController.doPrivileged(new PrivilegedAction<Class>() {//那么返回factoryBean的类型这个操作不做权限检查,直接调用 public Class run() { return factoryBean.getObjectType(); } }, getAccessControlContext()); } else {//不存在安全管理器,就直接调用! return factoryBean.getObjectType(); } } catch (Throwable ex) { 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; } } //根据FactoryBean名,返回其生产的Object,从缓存中取 protected Object getCachedObjectForFactoryBean(String beanName) { Object object = this.factoryBeanObjectCache.get(beanName); return (object != NULL_OBJECT ? object : null); } //从工厂Bean中取实例,实际调用下面的doGetObjectFromFactoryBean方法。 protected Object getObjectFromFactoryBean(FactoryBean factory, String beanName, boolean shouldPostProcess) { if (factory.isSingleton() && containsSingleton(beanName)) {//若工厂是单例,且本容器包含beanName对应的单例类 synchronized (getSingletonMutex()) {//以所有的单例集合为锁 Object object = this.factoryBeanObjectCache.get(beanName);//根据beanName从factoryBeanObjectCache中取 if (object == null) {//若取不到 object = doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess); this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));//放进factoryBeanObjectCache } return (object != NULL_OBJECT ? object : null); } } else {//否则,直接 return doGetObjectFromFactoryBean(factory, beanName, shouldPostProcess); } } //从工厂Bean中取实例 private Object doGetObjectFromFactoryBean( final FactoryBean factory, final String beanName, final boolean shouldPostProcess) throws BeanCreationException { Object object; //跟getTypeForFactoryBean的实现一样。 try { if (System.getSecurityManager() != null) {//若系统存在安全管理器 AccessControlContext acc = getAccessControlContext();//得到当前容器的安全访问上下文 try {//返回factoryBean的类型这个操作不做权限检查,直接调用 object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { 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); } //如果从beanFactory取不到,且这个实例即将被创建,抛出异常 if (object == null && isSingletonCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException( beanName, "FactoryBean which is currently in creation returned null from getObject"); } //若取不到,且这个实例允许前处理 if (object != null && shouldPostProcess) { try { object = postProcessObjectFromFactoryBean(object, beanName);//这里简单返回,前处理的功能留给子类重写 } catch (Throwable ex) { throw new BeanCreationException(beanName, "Post-processing of the FactoryBean's object failed", ex); } } return object; } //这里简单返回Object,留给子类重写 protected Object postProcessObjectFromFactoryBean(Object object, String beanName) throws BeansException { return object; } //如果这个Object是FactoryBean类型,就转换成FactoryBean返回 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; } //移除单例类这个方法重写,父类的移除之外,还要移除factoryBeanObjectCache中的。 @Override protected void removeSingleton(String beanName) { super.removeSingleton(beanName); this.factoryBeanObjectCache.remove(beanName); } //返回当前容器的安全访问上下文 protected AccessControlContext getAccessControlContext() { return AccessController.getContext(); } }