Spring IOC 源码学习 环境准备

BeanDefinition

BeanDefinition 主要是用来描述Bean,主要包括:Bean的属性、是否单例、延迟加载、Bean的名称、构造方法等。

简而言之: 在容器中的 一个Bean 对应一个 BeanDefinition 的封装描述; Spring 使用 BeanDefinitionRegistry 管理BeanDefinition

org.springframework.beans.factory.config.BeanDefinition

/*
 * Copyright 2002-2020 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
 *
 *      https://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.config;
 
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.core.AttributeAccessor;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
 
/**
 *
 *  BeanDefinition 描述一个bean实例,该实例具有属性值、构造函数参数值以及由具体实现提供的进一步信息。
 *  简而言之: 在容器中的Bean 对应一个 BeanDefinition的封装描述
 *
 * A BeanDefinition describes a bean instance, which has property values,
 * constructor argument values, and further information supplied by
 * concrete implementations.
 *
 * <p>This is just a minimal interface: The main intention is to allow a
 * {@link BeanFactoryPostProcessor} to introspect and modify property values
 * and other bean metadata.
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @since 19.03.2004
 * @see ConfigurableListableBeanFactory#getBeanDefinition
 * @see org.springframework.beans.factory.support.RootBeanDefinition
 * @see org.springframework.beans.factory.support.ChildBeanDefinition
 */
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
 
	/**
	 * Scope identifier for the standard singleton scope: {@value}.
	 * <p>Note that extended bean factories might support further scopes.
	 * Bean 的作用域标识: 单例标记, 可通过setScope方法设置
	 * MVC的Session Request 等不在这里..
	 * @see #setScope
	 * @see ConfigurableBeanFactory#SCOPE_SINGLETON
	 */
	String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
 
	/**
	 * Scope identifier for the standard prototype scope: {@value}.
	 * <p>Note that extended bean factories might support further scopes.
	 * Bean 的作用域标识: 原型标记, 可通过setScope方法设置
	 * MVC的Session Request 等不在这里..
	 * @see #setScope
	 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
	 */
	String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
 
	/**
	 * 以下三个属性, 标识 Bean 的角色, 可通过 setRole 方法设置
	 * ROLE_APPLICATION = 0; //应用程序定义, 或用户定义的bean
	 * ROLE_SUPPORT = 1;     //比较复杂的配置, 值得注意的? 目前没搞明白..
	 * ROLE_INFRASTRUCTURE = 2;//Spring内部定义的Bean
 	*/
 
	/**
	 * Role hint indicating that a {@code BeanDefinition} is a major part
	 * of the application. Typically corresponds to a user-defined bean.
	 */
	int ROLE_APPLICATION = 0;
 
	/**
	 * Role hint indicating that a {@code BeanDefinition} is a supporting
	 * part of some larger configuration, typically an outer
	 * {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
	 * {@code SUPPORT} beans are considered important enough to be aware
	 * of when looking more closely at a particular
	 * {@link org.springframework.beans.factory.parsing.ComponentDefinition},
	 * but not when looking at the overall configuration of an application.
	 */
	int ROLE_SUPPORT = 1;
 
	/**
	 * Role hint indicating that a {@code BeanDefinition} is providing an
	 * entirely background role and has no relevance to the end-user. This hint is
	 * used when registering beans that are completely part of the internal workings
	 * of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
	 */
	int ROLE_INFRASTRUCTURE = 2;
 
 
	// Modifiable attributes
 
	/**
	 * Set the name of the parent definition of this bean definition, if any.
	 * 设置 父Bean Definition 的名称, 如果有的话
	 * 对应XML定义: `<bean parent="">`
	 */
	void setParentName(@Nullable String parentName);
 
	/**
	 * Return the name of the parent definition of this bean definition, if any.
	 */
	@Nullable
	String getParentName();
 
	/**
	 * Specify the bean class name of this bean definition.
	 * <p>The class name can be modified during bean factory post-processing,
	 * typically replacing the original class name with a parsed variant of it.
	 *
	 *  bean的类名, 对应XML定义: `<bean class="">`
	 * @see #setParentName
	 * @see #setFactoryBeanName
	 * @see #setFactoryMethodName
	 */
	void setBeanClassName(@Nullable String beanClassName);
 
	/**
	 * Return the current bean class name of this bean definition.
	 * <p>Note that this does not have to be the actual class name used at runtime, in
	 * case of a child definition overriding/inheriting the class name from its parent.
	 * Also, this may just be the class that a factory method is called on, or it may
	 * even be empty in case of a factory bean reference that a method is called on.
	 * Hence, do <i>not</i> consider this to be the definitive bean type at runtime but
	 * rather only use it for parsing purposes at the individual bean definition level.
	 * @see #getParentName()
	 * @see #getFactoryBeanName()
	 * @see #getFactoryMethodName()
	 */
	@Nullable
	String getBeanClassName();
 
	/**
	 * Override the target scope of this bean, specifying a new scope name.
	 * @see #SCOPE_SINGLETON
	 * @see #SCOPE_PROTOTYPE
	 * 设置bean作用域
	 */
	void setScope(@Nullable String scope);
	/**
	 * Return the name of the current target scope for this bean,
	 * or {@code null} if not known yet.
	 */
	@Nullable
	String getScope();
 
	/**
	 * Set whether this bean should be lazily initialized.
	 * <p>If {@code false}, the bean will get instantiated on startup by bean
	 * factories that perform eager initialization of singletons.
	 * 设置是否懒加载
	 */
	void setLazyInit(boolean lazyInit);
 
	/**
	 * Return whether this bean should be lazily initialized, i.e. not
	 * eagerly instantiated on startup. Only applicable to a singleton bean.
	 */
	boolean isLazyInit();
 
	/**
	 * Set the names of the beans that this bean depends on being initialized.
	 * The bean factory will guarantee that these beans get initialized first.
	 *
	 *   dependsOn 一般用于两个bean之间没有显示依赖, 但后一个Bean需要用到前一个Bean执行初始方法后的结果;
	 *     例如在 `< bean id="a" dependsOn="b"/> ` 时, 在初始化a时首先先初始化b, 在销毁b之前会先销毁a;
	 */
	void setDependsOn(@Nullable String... dependsOn);
 
	/**
	 * Return the bean names that this bean depends on.
	 */
	@Nullable
	String[] getDependsOn();
 
	/**
	 * Set whether this bean is a candidate for getting autowired into some other bean.
	 * <p>Note that this flag is designed to only affect type-based autowiring.
	 * It does not affect explicit references by name, which will get resolved even
	 * if the specified bean is not marked as an autowire candidate. As a consequence,
	 * autowiring by name will nevertheless inject a bean if the name matches.
	 *
	 * 是否 能被自动装配 <bean autowire-candidate="true/false">
	 *
	 * 设置该对象是否可以被其他对象自动装配(可以作为自动注入的候选);  spring通过配置bean的autowire属性设置自动装配方式
	 */
	void setAutowireCandidate(boolean autowireCandidate);
 
	/**
	 * Return whether this bean is a candidate for getting autowired into some other bean.
	 */
	boolean isAutowireCandidate();
 
	/**
	 * Set whether this bean is a primary autowire candidate.
	 * <p>If this value is {@code true} for exactly one bean among multiple
	 * matching candidates, it will serve as a tie-breaker.
	 * `@Primary` 注解就是通过它去设置的; 如果存在同类型的 bean 是否这个bean优先
	 */
	void setPrimary(boolean primary);
 
	/**
	 * Return whether this bean is a primary autowire candidate.
	 */
	boolean isPrimary();
 
	/**
	 * Specify the factory bean to use, if any.
	 * This the name of the bean to call the specified factory method on.
	 * 定义创建该 Bean对象的工厂类, 对应XML定义: `<bean factory-bean="">`
	 * @see #setFactoryMethodName
	 */
	void setFactoryBeanName(@Nullable String factoryBeanName);
 
	/**
	 * Return the factory bean name, if any.
	 */
	@Nullable
	String getFactoryBeanName();
 
	/**
	 * Specify a factory method, if any. This method will be invoked with
	 * constructor arguments, or with no arguments if none are specified.
	 * The method will be invoked on the specified factory bean, if any,
	 * or otherwise as a static method on the local bean class.
	 *
	 * 定义创建该Bean对象的工厂方法, 对应XML定义 ` <bean factory-method="">`
	 * @see #setFactoryBeanName
	 * @see #setBeanClassName
	 */
	void setFactoryMethodName(@Nullable String factoryMethodName);
 
	/**
	 * Return a factory method, if any.
	 */
	@Nullable
	String getFactoryMethodName();
 
	/**
	 * Return the constructor argument values for this bean.
	 * <p>The returned instance can be modified during bean factory post-processing.
	 * 返回此bean的构造函数参数值;
	 * @return the ConstructorArgumentValues object (never {@code null})
	 */
	ConstructorArgumentValues getConstructorArgumentValues();
 
	/**
	 * Return if there are constructor argument values defined for this bean.
	 * @since 5.0.2
	 */
	default boolean hasConstructorArgumentValues() {
		return !getConstructorArgumentValues().isEmpty();
	}
 
	/**
	 * Return the property values to be applied to a new instance of the bean.
	 * <p>The returned instance can be modified during bean factory post-processing.
	 *
	 * 普通属性的集合, 如:
	 *
	 * <bean id="student" class="com.rc.sp.Student">
	 *   <property name="id" value="1"/>
	 *   <property name="dao" ref="dao" />
	 *   <property name="map">
	 *     <map>
	 *       <entry key="math" value="90"/>
	 *       <entry key="english" value="85"/>
	 *     </map>
	 *   </property>
	 * </bean>
	 *
	 * @return the MutablePropertyValues object (never {@code null})
	 */
	MutablePropertyValues getPropertyValues();
 
	/**
	 * Return if there are property values defined for this bean.
	 * @since 5.0.2
	 */
	default boolean hasPropertyValues() {
		return !getPropertyValues().isEmpty();
	}
 
	/**
	 * Set the name of the initializer method.
	 * 初始化方法名 对应XML 定义: `<bean id="address" class="org.yang.learn.spring.dto.Address" init-method="" destroy-method="">`
	 * @since 5.1
	 */
	void setInitMethodName(@Nullable String initMethodName);
 
	/**
	 * Return the name of the initializer method.
	 * @since 5.1
	 */
	@Nullable
	String getInitMethodName();
 
	/**
	 * Set the name of the destroy method.
	 * 销毁方法名 对应XML 定义: `<bean id="address" class="org.yang.learn.spring.dto.Address" init-method="" destroy-method="">`
	 * @since 5.1
	 */
	void setDestroyMethodName(@Nullable String destroyMethodName);
 
	/**
	 * Return the name of the destroy method.
	 * @since 5.1
	 */
	@Nullable
	String getDestroyMethodName();
 
	/**
	 * Set the role hint for this {@code BeanDefinition}. The role hint
	 * provides the frameworks as well as tools an indication of
	 * the role and importance of a particular {@code BeanDefinition}.
	 * @since 5.1
	 * @see #ROLE_APPLICATION
	 * @see #ROLE_SUPPORT
	 * @see #ROLE_INFRASTRUCTURE
	 */
	void setRole(int role);
 
	/**
	 * Get the role hint for this {@code BeanDefinition}. The role hint
	 * provides the frameworks as well as tools an indication of
	 * the role and importance of a particular {@code BeanDefinition}.
	 * @see #ROLE_APPLICATION
	 * @see #ROLE_SUPPORT
	 * @see #ROLE_INFRASTRUCTURE
	 */
	int getRole();
 
	/**
	 * Set a human-readable description of this bean definition.
	 * @since 5.1
	 */
	void setDescription(@Nullable String description);
 
	/**
	 * Return a human-readable description of this bean definition.
	 */
	@Nullable
	String getDescription();
 
 
	// Read-only attributes
 
	/**
	 * Return a resolvable type for this bean definition,
	 * based on the bean class or other specific metadata.
	 * <p>This is typically fully resolved on a runtime-merged bean definition
	 * but not necessarily on a configuration-time definition instance.
	 *
	 * 这个 ResolvableType 作用, 大概是封装java的类型描述
	 * ResolvableTypes may be obtained from fields, method parameters, method returns or classes.
	 * 	  For example:
	 * 		private HashMap<Integer, List<String>> myMap;
	 * 		public void example() {
	 * 			ResolvableType t = ResolvableType.forField(getClass().getDeclaredField("myMap"));
	 * 			t.getSuperType(); // AbstractMap<Integer, List<String>>
	 * 			t.asMap(); // Map<Integer, List<String>>
	 * 			t.getGeneric(0).resolve(); // Integer
	 * 			t.getGeneric(1).resolve(); // List
	 * 			t.getGeneric(1); // List<String>
	 * 			t.resolveGeneric(1, 0); // String
	 * 		}
	 * @return the resolvable type (potentially {@link ResolvableType#NONE})
	 * @since 5.2
	 * @see ConfigurableBeanFactory#getMergedBeanDefinition
	 */
	ResolvableType getResolvableType();
 
	/**
	 * Return whether this a <b>Singleton</b>, with a single, shared instance
	 * returned on all calls.
	 * @see #SCOPE_SINGLETON
	 */
	boolean isSingleton();
 
	/**
	 * Return whether this a <b>Prototype</b>, with an independent instance
	 * returned for each call.
	 * @since 3.0
	 * @see #SCOPE_PROTOTYPE
	 */
	boolean isPrototype();
 
	/**
	 * Return whether this bean is "abstract", that is, not meant to be instantiated.
	 */
	boolean isAbstract();
 
	/**
	 * Return a description of the resource that this bean definition
	 * came from (for the purpose of showing context in case of errors).
	 *
	 * 返回该bean定义来自的资源的描述(用于在出现错误时显示上下文)
	 */
	@Nullable
	String getResourceDescription();
 
	/**
	 * Return the originating BeanDefinition, or {@code null} if none.
	 * <p>Allows for retrieving the decorated bean definition, if any.
	 * <p>Note that this method returns the immediate originator. Iterate through the
	 * originator chain to find the original BeanDefinition as defined by the user.
	 * 返回原始 BeanDefinition 定义; 可null
	 */
	@Nullable
	BeanDefinition getOriginatingBeanDefinition();
 
}
 
 

关于 role 属性

getRole 属性 ; Spring将bean分为三种角色

  1. ROLE_APPLICATION = 0;//应用程序定义, 或用户定义的bean
  2. ROLE_SUPPORT = 1; //比较复杂的配置, 值得注意的? 目前没搞明白
  3. ROLE_INFRASTRUCTURE = 2;//Spring内部定义的Bean, 与用户完全无关

BeanDefinition 的常见实现

  1. GenericBeanDefinitionBeanDefinition 接口的通用实现,可以用于大多数 bean 定义情况。它提供了许多属性和方法,可以用来配置 bean 的各种属性和行为。

  2. RootBeanDefinitionGenericBeanDefinition 的子类,提供了更多针对父子 bean 配置的支持。它可以用于定义根 bean,同时允许其他 bean 对其进行扩展。

  3. ChildBeanDefinition: 同样是 GenericBeanDefinition 的子类,用于定义子 bean。它可以继承另一个 bean 的配置,并可以进行进一步的定制和覆盖。

  4. AnnotatedBeanDefinition 这是用于 基于注解的配置的 BeanDefinition 实现。它可以用来描述通过注解(如 @Component@Service@Repository 等)定义的 bean。