Spring源码-AbstractBeanFactory-源码解读


/**
 * Abstract base class for {@link org.springframework.beans.factory.BeanFactory}			BeanFactory 接口的抽象类实现
 * implementations, providing the full capabilities of the									提供了 ConfigurableBeanFactory 的 spi
 * {@link org.springframework.beans.factory.config.ConfigurableBeanFactory} SPI.
 * Does <i>not</i> assume a listable bean factory: can therefore also be used				不可以认为是一个 listable bean factory:
 * as base class for bean factory implementations which obtain bean definitions				因此也可以用作 bean factory 的基类,
 * from some backend resource (where bean definition access is an expensive operation).		可以从一些后台资源去获取 bean definitions (其中 bean definition 的访问是一项昂贵的操做)
 *
 * <p>This class provides a singleton cache (through its base class							这个类提供了一个单例缓存(通过它的基类 DefaultSingletonBeanRegistry ),
 * {@link org.springframework.beans.factory.support.DefaultSingletonBeanRegistry},
 * singleton/prototype determination, {@link org.springframework.beans.factory.FactoryBean}	确定单例或原型,FactoryBean 处理
 * handling, aliases, bean definition merging for child bean definitions,					别名,子 bean definitions 的合并
 * and bean destruction ({@link org.springframework.beans.factory.DisposableBean}			和 bean 销毁 (DisposableBean 接口)
 * interface, custom destroy methods). Furthermore, it can manage a bean factory			自定义销毁方法
 * hierarchy (delegating to the parent in case of an unknown bean), through implementing	此外,它能够管理 bean factory 的层次(在未知 bean 的情况下委托为父亲)
 * the {@link org.springframework.beans.factory.HierarchicalBeanFactory} interface.			通过实现 HierarchicalBeanFactory 接口
 *
 * <p>The main template methods to be implemented by subclasses are							主要的模版方法 getBeanDefinition 和 createBean 通过子类实现 ,
 * {@link #getBeanDefinition} and {@link #createBean}, retrieving a bean definition			分别是为给定的 bean name 检索一个 bean definition
 * for a given bean name and creating a bean instance for a given bean definition,			和为给定的 bean definition 创建 bean 实例
 * respectively. Default implementations of those operations can be found in				这些操作的默认实现可以在 DefaultListableBeanFactory DefaultListableBeanFactory 中建立
 * {@link DefaultListableBeanFactory} and {@link AbstractAutowireCapableBeanFactory}.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Costin Leau
 * @author Chris Beams
 * @since 15 April 2001
 * @see #getBeanDefinition
 * @see #createBean
 * @see AbstractAutowireCapableBeanFactory#createBean
 * @see DefaultListableBeanFactory#getBeanDefinition
 */
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
	//---------------------------------------------------------------------
	// Implementation of BeanFactory interface
	//---------------------------------------------------------------------

	@Override
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}

	@Override
	public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
		return doGetBean(name, requiredType, null, false);
	}

	@Override
	public Object getBean(String name, Object... args) throws BeansException {
		return doGetBean(name, null, args, false);
	}

	/**
	 * Return an instance, which may be shared or independent, of the specified bean.		返回一个实例,可以是共享的也可以是单独的,指定的 bean
	 * @param name the name of the bean to retrieve
	 * @param requiredType the required type of the bean to retrieve
	 * @param args arguments to use when creating a bean instance using explicit arguments
	 * (only applied when creating a new instance as opposed to retrieving an existing one)
	 * @return an instance of the bean
	 * @throws BeansException if the bean could not be created
	 */
	public <T> T getBean(String name, @Nullable Class<T> requiredType, @Nullable Object... args)
			throws BeansException {

		return doGetBean(name, requiredType, args, false);
	}

	/**
	 * Return an instance, which may be shared or independent, of the specified bean.
	 * @param name the name of the bean to retrieve
	 * @param requiredType the required type of the bean to retrieve
	 * @param args arguments to use when creating a bean instance using explicit arguments
	 * (only applied when creating a new instance as opposed to retrieving an existing one)
	 * @param typeCheckOnly whether the instance is obtained for a type check, 是否对获得的实例进行类型检查
	 * not for actual use
	 * @return an instance of the bean
	 * @throws BeansException if the bean could not be created
	 */
	@SuppressWarnings("unchecked")
	protected <T> T doGetBean(
			String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
			throws BeansException {}

	/**
	 * Return a 'merged' BeanDefinition for the given bean name,				对于一个给定的 bean name,返回一个合并后的 BeanDefinition
	 * merging a child bean definition with its parent if necessary.			如果有必要将子 bean definition 和他的父亲合并
	 * <p>This {@code getMergedBeanDefinition} considers bean definition		getMergedBeanDefinition 在他的祖先身上也是如此
	 * in ancestors as well.
	 * @param name the name of the bean to retrieve the merged definition for
	 * (may be an alias)
	 * @return a (potentially merged) RootBeanDefinition for the given bean
	 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
	 * @throws BeanDefinitionStoreException in case of an invalid bean definition
	 */
	@Override
	public BeanDefinition getMergedBeanDefinition(String name) throws BeansException {}

	/**
	 * Callback before prototype creation.												原型创建之前的回调
	 * <p>The default implementation register the prototype as currently in creation.	默认注册实现是将原型放入正在注册的原型当中
	 * @param beanName the name of the prototype about to be created
	 * @see #isPrototypeCurrentlyInCreation
	 */
	@SuppressWarnings("unchecked")
	protected void beforePrototypeCreation(String beanName) {}


	/**
	 * Callback after prototype creation.												原型创建后回调
	 * <p>The default implementation marks the prototype as not in creation anymore.	默认实现是标记原型不再处于创建中
	 * @param beanName the name of the prototype that has been created
	 * @see #isPrototypeCurrentlyInCreation
	 */
	@SuppressWarnings("unchecked")
	protected void afterPrototypeCreation(String beanName) {}

	/**
	 * Return the bean name, stripping out the factory dereference prefix if necessary,
	 * and resolving aliases to canonical names. 返回 bean name ,如果有必要脱去工厂废弃前缀,并且根据规范去解析别名
	 * @param name the user-specified name
	 * @return the transformed bean name
	 */
	protected String transformedBeanName(String name) {
		return canonicalName(BeanFactoryUtils.transformedBeanName(name));
	}

	/**
	 * Initialize the given BeanWrapper with the custom editors registered	使用自定义的 editors registered 初始化给定的 BeanWrapper
	 * with this factory. To be called for BeanWrappers that will create	被调用 BeanWrappers 将创建并填充 bean 实例
	 * and populate bean instances.
	 * <p>The default implementation delegates to {@link #registerCustomEditors}.
	 * Can be overridden in subclasses.
	 * @param bw the BeanWrapper to initialize
	 */
	protected void initBeanWrapper(BeanWrapper bw) {
		bw.setConversionService(getConversionService());
		registerCustomEditors(bw);
	}

	/**
	 * Return a merged RootBeanDefinition, traversing the parent bean definition 返回一个合并的 RootBeanDefinition,
	 * if the specified bean corresponds to a child bean definition. 如果指定的 bean 相当于一个 子 bean 定义,遍历它的父 bean definition
	 * @param beanName the name of the bean to retrieve the merged definition for
	 * @return a (potentially merged) RootBeanDefinition for the given bean
	 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
	 * @throws BeanDefinitionStoreException in case of an invalid bean definition
	 */
	protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
		// Quick check on the concurrent map first, with minimal locking. // 首先快速检查这个 concurrent map,伴随着极小的锁过程
		RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
		if (mbd != null) {
			return mbd;
		}
		return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
	}

	/**
	 * Create a bean instance for the given merged bean definition (and arguments).		对于给定的 bean definition(和参数) 创建一个 bean 实例
	 * The bean definition will already have been merged with the parent definition		child definition 已经与 child definition 合并的情况下
	 * in case of a child definition.
	 * <p>All bean retrieval methods delegate to this method for actual bean creation.	所有的 bean 检索方法都将委派给这个方法去创建 bean
	 * @param beanName the name of the bean
	 * @param mbd the merged bean definition for the bean
	 * @param args explicit arguments to use for constructor or factory method invocation
	 * @return a new instance of the bean
	 * @throws BeanCreationException if the bean could not be created
	 */
	protected abstract Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException;

}

为上层接口 ConfigurableBeanFactory 提供对应的容器和实例

主要方法

方法名含义
protected abstract Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)对于给定的 bean definition(和参数) 创建一个 bean 实例,所有的 bean 检索方法都将委派给这个方法去创建 bean
protected void afterPrototypeCreation(String beanName)原型创建后回调
protected void beforePrototypeCreation(String beanName)protected void beforePrototypeCreation(String beanName)
public BeanDefinition getMergedBeanDefinition(String name)对于一个给定的 bean name,返回一个合并后的 BeanDefinition
protected T doGetBean(String name, @Nullable Class requiredType, @Nullable Object[] args, boolean typeCheckOnly)实际创建 bean 的模版方法

主要属性

属性名含义
private final Map<String, Scope> scopes生命周期
private final Map<String, RootBeanDefinition> mergedBeanDefinitions(bean name,被合并过的 RootBeanDefinition)
private final Set alreadyCreated至少已经被创建了一次的 bean name
private final ThreadLocal prototypesCurrentlyInCreation当前正在被创建的 bean 的 names 原型
private final List embeddedValueResolvers应用于注解属性值等的字符串解析程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值